Android + ORMLite + java.sql.Timestamp

邮差的信 提交于 2019-12-11 11:27:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!