为了用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.
总结以上是内存溢出为你收集整理的android-ORMLite多对多关系全部内容,希望文章能够帮你解决android-ORMLite多对多关系所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)