ORMLite - 处理我从JSON源获得的多对多关系

时间:2014-03-19 16:26:28

标签: java android json gson ormlite

et's说我有两个API调用。一个人得到Foos引用一些酒吧:

{"foos":[{"id":1,"barIDs":[4,5,6]},{"id":2,"barIDs":[1,2,3]}]}

其他人获得引用Foos的条:

{"bars":[{"id":1,"fooIDs":[4,5,6]},{"id":2,"fooIDs":[1,2,3]}]}

GSON说得对,现在我有两个Foo和两个Bar对象,它们都引用了一系列外来ID。现在我试图让这个持久化,但没有运气这样做,因为我只有ID引用,而不是真正的对象。我和Foo达成了这个目标:

@DatabaseTable(tableName = "foos")
public class Foo {
    @DatabaseField(id = true, columnName = "id")
    private long id;

    public ArrayList<Long> barIDs;

    @ForeignCollectionField(eager = false, foreignFieldName = "barIDs")
    public ForeignCollection<Bar> bars;
}

(Bar几乎相同)

当我尝试运行代码时,这就是我得到的:

java.sql.SQLException: Foreign collection class com.example.ormlite.entities.Bar
    for field 'bars' column-name does not contain a foreign field named 'barIDs'
    of class com.example.ormlite.entities.Foo

所以我尝试了许多不同的交叉注释组合,填充了各个领域,但显然,我仍然不明白它是如何工作的。你们有没有想过如何处理我只有ID引用而不是里面真正的孩子的情况?我开始对ORMLite有点绝望,所以如果我做不到,我会直接使用SQLite,但我真的很想学习如何使用它。

谢谢!

0 个答案:

没有答案
相关问题