简体   繁体   English

Android + ORMLite + java.sql.Timestamp

[英]Android + ORMLite + java.sql.Timestamp

I have an Android project which uses ORMLite. 我有一个使用ORMLite的Android项目。 All works fine, but for some fields in database it uses java.sql.Timestamp as field type. 一切正常,但是对于数据库中的某些字段,它使用java.sql.Timestamp作为字段类型。 It worked fine on ORMLite 4.10, which I used before. 在我以前使用的ORMLite 4.10上,它运行良好。 But now I need to update ORMLite, and have a problem.. When I updated ORMLite to newer version and run it, app throws exception: 但是现在我需要更新ORMLite,并且有一个问题。当我将ORMLite更新为较新的版本并运行它时,应用程序将引发异常:

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. 之前没有createDate字段的dataType参数。 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. 但是,当我添加它时,错误并没有得到修复,应用程序也会引发此异常:(。该项目已经在Market上,因此我无法将dataType更改为其他类型(例如Date),也可以删除此字段。

What should I use for Timestamp on newer ORMLite? 在较新的ORMLite上,我应该为Timestamp使用什么? Or it is ORMLite bug? 还是ORMLite错误?

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;
}

Sorry about this @st1ing. 对不起,这个@ st1ing。 This certainly can be a bug in ORMLite but I suspect rather that it may be an incompatibility. 这肯定是ORMLite中的错误,但我怀疑这可能是不兼容的。 In version 4.14, we had to make a change how Serializable fields are handled. 在版本4.14中,我们必须更改如何处理可Serializable字段。 Upgrade notes are online. 升级说明在线。 To quote: 报价:

In addition, serialized types must also now specify their dataType value. 此外,序列化类型现在还必须指定其dataType值。 You should use DataType.SERIALIZABLE to continue to store serialized objects in the database. 您应该使用DataType.SERIALIZABLE继续将序列化的对象存储在数据库中。 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: 如果您已经在数据库中存储了可Serializable数据( byte[]或其他对象),则需要在字段中添加如下所示的内容:

@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. 因此,请尝试将其添加到您的Timestamp字段。 Let me know if it works. 让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM