当字段可以是多种类型时,在 POJO 类中定义数据类型

发布于 2024-12-04 23:28:44 字数 591 浏览 1 评论 0 原文

我在我的应用程序中使用ormlite android,我有一个表,其中有一列可以存储三种类型的数据,这些数据实际上是三个不同类的对象。我想在我的pojo类中声明该字段的数据类型,我也尝试使用对象,但仍然显示错误。Ormlite 不理解对象数据类型。

ormlite 是否提供对此类功能的支持?

编辑1

这是我的pojo类

@DatabaseTable(tableName = "itinerary_item")
public class ItineraryItem {
@DatabaseField(columnName = "id", id = true)
private int mId;

@DatabaseField(dataType = DataType.SERIALIZABLE, columnName = "item_type", foreign = true, foreignAutoRefresh = true)
private Object mItem;

,其中这个mitem可以是数据库中3种不同的类型,这基本上是三个不同POJO类的对象。 但我的问题是 ORMLite 不支持对象数据类型。

I am using ormlite android in my app and i have a table which have a column which can store three type of data which are actually the object of three different class.I want to declare data type of that field in my pojo class and i also tried with Object but still it is showing error.Ormlite does not understand Object data type.

Does ormlite provide any support for such functionality?

Edit 1

This is my pojo class

@DatabaseTable(tableName = "itinerary_item")
public class ItineraryItem {
@DatabaseField(columnName = "id", id = true)
private int mId;

@DatabaseField(dataType = DataType.SERIALIZABLE, columnName = "item_type", foreign = true, foreignAutoRefresh = true)
private Object mItem;

Where this mitem can be of 3 different Type in the database which is basically the object of three different POJO class.
But my problem is that ORMLite is not supporting the Object data type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-12-11 23:28:44

请提供您尝试保留的对象的代码示例,并列出 完整异常>ORMLite 正在抛出。


但与此同时,我可以谈谈 ORMLite 可以保留哪些类型。这是完整列表:

http://ormlite.com/docs/data-types

它将检测各种字段类型。对于列表中特定的对象,ORMLite 还支持实现 Serialized 的持久对象。对于这些,您必须使用 @DatabaseField(dataType = DataType.SERIALIZABLE) 专门声明类型。

http://ormlite.com/docs/serialized

最后,对于高级用户,您可以定义并注册您 带有 @DatabaseField(persisterClass = ....class) 代码的自己的持久化类。


编辑:

我建议如果 mitem 是 3 种类型之一,那么您有 3 个单独的字段,每个字段都有一个明确的类型 - 不要使用 Object.然后,对于 pojo 类的每个实例,将设置 3 个字段中的 1 个,其他字段将为空。

Please provide a sample of code from the object you are trying to persist and please list the full exception that ORMLite is throwing.


But in the meantime I can talk a bit about what types ORMLite can persist. Here's the full list:

http://ormlite.com/docs/data-types

It will detect the various fields types. For objects that are specifically on the list, ORMLite also supports persisting objects that implement Serializable. For these you have to specifically declare the type with @DatabaseField(dataType = DataType.SERIALIZABLE).

http://ormlite.com/docs/serializable

Lastly, for advanced users, you can define and register your own persister class with the @DatabaseField(persisterClass = ....class) code.


Edit:

I would suggest if mitem is one of 3 types then you have 3 separate fields, each with a definitive type -- don't use Object. Then for every instance of your pojo class, 1 of the 3 fields will be set and the others will be null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文