我正在尝试使用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;
...
}
我收到了这个错误:
'comments'的字段类必须是ForeignCollection类或 收藏
我也在使用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实现。
答案 0 :(得分:1)
在评论课中:
...
@DatabaseField(
foreign = true
)
private Conversation conversation_id;
...
conversation_id实际上只存储Conversation对象的id,而不是对象本身。
这里有一个非常好的(如果以某种方式未格式化)文档:http://ormlite.com/docs/foreign-object