问题
I have an Android project which uses ORMLite. All works fine, but for some fields in database it uses java.sql.Timestamp
as field type. It worked fine on ORMLite 4.10, which I used before. But now I need to update ORMLite, and have a problem..
When I updated ORMLite to newer version and run it, app throws exception:
W/System.err( 615): java.sql.SQLException: ORMLite can't store unknown class
class java.sql.Timestamp for field 'createDate'.
Serializable fields must specify dataType=DataType.SERIALIZABLE
There was no dataType parameter for createDate field before. But when I added it, error is not fixed and app throws this exception too :(. The project is already on Market, so I can't change dataType to other type (such as Date), and can remove this field too.
What should I use for Timestamp on newer ORMLite? Or it is ORMLite bug?
UPDATE: Fixed it by changing Timestamp type to Serializable type. Now code looks like this:
@DatabaseField(dataType = DataType.SERIALIZABLE)
public Serializable createDate;
public Timestamp getCreateDate()
{
return (Timestamp)createDate;
}
回答1:
Sorry about this @st1ing. This certainly can be a bug in ORMLite but I suspect rather that it may be an incompatibility. In version 4.14, we had to make a change how Serializable
fields are handled. Upgrade notes are online. To quote:
In addition, serialized types must also now specify their dataType value. You should use
DataType.SERIALIZABLE
to continue to store serialized objects in the database. This will allow us to add direct support for other serialized types in the future without breaking backwards compatibility.If you already have
Serializable
data (byte[]
or other objects) stored in a database then you will need to add something like the following to your fields:
@DatabaseField(dataType = DataType.SERIALIZABLE)
Serializable field;
Actually, the exception is trying to give you a hint as to how to fix you problem:
Serializable fields must specify dataType=DataType.SERIALIZABLE
So try adding that to your Timestamp
field. Let me know if it works.
来源:https://stackoverflow.com/questions/10186705/android-ormlite-java-sql-timestamp