我在android上使用Ormlite进行类似于以下结构的结构:
Book类具有主要的BookArticle集合和次要的BookArticle集合:
@Databasetable(tablename = "BookV1", daoClass = BookDaoImplV1.class)public class Book implements IBook { @DatabaseFIEld(ID = true) private String ID; @ForeignCollectionFIEld(eager = false) private ForeignCollection<BookArticle> primaryArticles; @ForeignCollectionFIEld(eager = false) private ForeignCollection<BookArticle> secondaryArticles; // constructor getters setters etc...}
然后将BookArticle声明如下:
@Databasetable(tablename = "BookArticleV1", daoClass = BookArticleDaoImplV1.class)public class BookArticle implements IBookArticle { @DatabaseFIEld(ID = true) private String ID; @DatabaseFIEld private String Title; @DatabaseFIEld private String summary; // for ORM mapPing only @DatabaseFIEld(foreign = true, foreignautoRefresh = true, columnname = BOOK_FIELDname, index = true) private Book book; // constructor getters setters etc..}
现在使用DAO保存BookArticles和Book可以很好地工作,但是当我想从数据库中获取Book实体并访问主要或次要文章(在refresh()之后)时,我遇到了问题,因为两个馆藏都保存了所有文章(主要和次要)二级),其“图书”列中包含该图书的ID.
显然,从数据库中获取时,我需要将这些主要文章和辅助文章分开.
我本来希望“ foreignFIEldname”是我的问题的答案.
@ForeignCollectionFIEld(eager = false, foreignFIEldname = "secondaryArticles")private ForeignCollection<BookArticle> secondaryArticles;
但显然,这种方式行不通.
有没有办法让我区分这两个系列?
也许使用简单的注释参数(例如“ owningFIEldname”或类似的参数)与BookArticle数据一起保留在DB中?
非常感谢您的帮助.
亚历克斯
解决方法:
我遇到了同样的问题.
这不是一个真正的解决方案,但是您可以尝试仅使用一个带有布尔值的列表来在主要/次要文章之间进行切换,如下所示:
@Databasetable(tablename = "BookV1", daoClass = BookDaoImplV1.class)public class Book implements IBook { @DatabaseFIEld(ID = true) private String ID; @ForeignCollectionFIEld(eager = false) private ForeignCollection<BookArticle> articles; // constructor getters setters etc...}
然后是BookArticle类
@Databasetable(tablename = "BookArticleV1", daoClass = BookArticleDaoImplV1.class)public class BookArticle implements IBookArticle { @DatabaseFIEld(ID = true) private String ID; @DatabaseFIEld private String Title; @DatabaseFIEld private String summary; @DatabaseFIEld private boolean isSecondary; // for ORM mapPing only @DatabaseFIEld(foreign = true, foreignautoRefresh = true, columnname = BOOK_FIELDname, index = true) private Book book; // constructor getters setters etc..}
希望看到一个真正的答案
总结以上是内存溢出为你收集整理的android-ORMLite中相同类型的多个ForeignCollection全部内容,希望文章能够帮你解决android-ORMLite中相同类型的多个ForeignCollection所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)