【问题标题】:Android DBFlow One to Many complete exampleAndroid DBFlow 一对多完整示例
【发布时间】:2017-05-02 14:01:11
【问题描述】:

我对如何在 DBFlow 中声明外键感到困惑。在 DBFlow (https://github.com/Raizlabs/DBFlow/blob/f2d90db39a4bf5ffcc0f22032ae20d5328b8d3c3/usage2/Relationships.md) 的关系入门页面中,它有 Queen 类的示例,但没有 AntColony 类。

有人有包含 Queen + Ant + Colony 类的完整示例吗?

谢谢

【问题讨论】:

    标签: java android orm dbflow


    【解决方案1】:

    这是您想要的示例代码:

    父表:

    @Table(database = AppDatabase.class)
    public class CafeWebContentCategories extends BaseModel {
        @PrimaryKey
        private int id;
    
        @Column
        private int categoryServerId;
    
        @PrimaryKey
        @Column
        public int cafeServerId;
    
        @Column
        private String title;
    
        @Column
        private boolean isAuto;
    
        public List<CafeWebChildContentCategories> subContentCategories;
    
        @OneToMany(methods = {OneToMany.Method.ALL}, variableName = "subContentCategories")
        public List<CafeWebChildContentCategories> getMyChannelSubContentCategories() {
            if (subContentCategories == null || subContentCategories.isEmpty()) {
                subContentCategories = SQLite.select()
                        .from(CafeWebChildContentCategories.class)
                        .where(CafeWebChildContentCategories_Table.parentId.eq(cafeServerId))
                        .queryList();
            }
            return subContentCategories;
        }
    
        public CafeWebContentCategories() {
        }
    
        // setters and getters
    }
    

    子表:

    @Table(database = AppDatabase.class)
    public class CafeWebChildContentCategories extends BaseModel {
        @PrimaryKey
        @Column
        public int id;
    
        @Column
        public int categoryId;
    
        @Column
        @ForeignKey(tableClass = CafeWebContentCategories.class,
                references = @ForeignKeyReference(columnName = "parentId", foreignKeyColumnName = "cafeServerId"))
        public int parentId;
    
        @Column
        private String title;
    
        public CafeWebChildContentCategories() {
        }
    
        // setters and getters
    }
    

    如果您对此有任何疑问,请询问我

    【讨论】:

    • 嗨,马赫迪,看起来不错,所以我接受了您的回答。不幸的是,你的例子来晚了。我决定改用领域。
    • @Sam 不要使用realm,因为每次发布新版本的领域大小都比旧版本大,我将所有项目从那里迁移到 dbflow,dfblow 比领域非常有用
    • 嗨 Mahdi,我尝试使用 DBFlow,因为我读到它非常快。但是,很难获得 DBFlow 的支持。文档也不是很好。但当 DBFlow 更成熟时,我肯定会研究它:)
    • @Sam sqlite 和包装库,因为 DBFlow 比领域慢。我必须在我的大项目中切换到领域
    猜你喜欢
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2014-03-13
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多