android-ORMLite多对多关系
作者:互联网
为了用ORMLite在这两个类之间建立多对多关系:
@DatabaseTable(tableName = "test1")
public class Test1 {
@ForeignCollectionField
private ForeignCollection<Test2> test2Collection;
}
@DatabaseTable(tableName = "test2")
public class Test2 {
@ForeignCollectionField
private ForeignCollection<Test1> test1Collection;
}
我面临的问题是在创建表时使用ORMLite,不知道此类之间的外键.
为了建立这种关系,我必须像这样在每个类上添加一个ForeignDatabaseField:
@DatabaseTable(tableName = "test1")
public class Test1 {
@DatabaseField(foreign = true, foreignAutoRefresh = true)
private Test2 test2;
@ForeignCollectionField
private ForeignCollection<Test2> test2Collection;
}
@DatabaseTable(tableName = "test2")
public class Test2 {
@DatabaseField(foreign = true, foreignAutoRefresh = true)
private Test1 test1;
@ForeignCollectionField
private ForeignCollection<Test1> test2Collection;
}
这似乎是一种奇怪的方式?
解决方法:
In order to make this relation do I have to add a single ForeignDatabaseField on each class like that:
对于多对多关系,将对象关联在一起的最佳方法是使用“联接表”.在您的情况下,将有一个名为Test1Test2Join的第三个表.
@DatabaseTable(tableName = "test1test2join")
public class Test1Test2Join {
@DatabaseField(generatedId = true)
private long id;
@DatabaseField(foreign = true)
private Test1 test1;
@DatabaseField(foreign = true)
private Test test2;
}
有关更多信息,请参见many-to-many example.
标签:many-to-many,dao,ormlite,android 来源: https://codeday.me/bug/20191123/2064468.html