![](/img/trans.png)
[英]How to represent set of java objects as a field of other object in database?
[英]How to represent List of Objects field in a model with ORMLite?
我正在尝试使用ORMLite来表示对话中的 评论 ,如下所示:
@DatabaseTable(tableName = "comments")
public class Comment implements Parcelable{
@DatabaseField(id = true)
private Long id;
@DatabaseField
private Long conversation_id;
@DatabaseField
private String text;
...
public static class List extends ArrayList<Comment>{
}
}
...和...
@DatabaseTable(tableName = "conversations")
public class Conversation implements Parcelable{
@DatabaseField(id = true)
private Long id;
...
@ForeignCollectionField
private Comment.List comments;
@DatabaseField
private Date created_at;
...
}
我收到此错误:
“评论”的字段类必须为ForeignCollection或Collection类
我也在使用GSON,因此这些模型是从json自动填充的。 例如:
{
"created_at":"2013-08-12T20:38:11Z",
"id":31,
"comments":[
{
"conversation_id":31,
"id":46,
"text":"IE sucks",
},
{
"conversation_id":31,
"id":47,
"text":"Yes it does",
}
]
}
有没有一种方法可以通过更改描述符来实现?
是否需要对Conversation类进行重做以将ForeignCollection用作注释类型,或者更改Comment.List类以扩展ForeignCollection? 我想避免这样做,因为我担心这会破坏目前运行良好的GSON实现。
在评论课中:
...
@DatabaseField(
foreign = true
)
private Conversation conversation_id;
...
session_id实际上仅存储对话对象的ID,而不存储对象本身。
这里有一个非常不错的文档(如果没有格式的话): http : //ormlite.com/docs/foreign-object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.