How to create a table with a two or more foreign keys using Android Room?

前端 未结 2 1979
孤独总比滥情好
孤独总比滥情好 2020-12-31 09:34

According the entity-relationship model, the relationship between tbl_post and tbl_category could be specified using Room Persistency Libr

2条回答
  •  攒了一身酷
    2020-12-31 09:52

    TblCategory.java

    @Entity
    class TblCategory {
        @PrimaryKey
        @ColumnInfo(name="cat_id")
        public String id;
    
        @ColumnInfo(name = "cat_name")
        public String name;
    }
    

    TblPost.java (It is missing the foreign key reference but it is not important for the case)

    @Entity
    class TblPost {
        @PrimaryKey
        @ColumnInfo(name="post_id")
        public String id;
    
        public String title, content, create_time, author_id;
    }
    

    TblPostCategory.java

    @Entity(foreignKeys = {
        @ForeignKey(
            entity = TblPost.class,
            parentColumns = "post_id",
            childColumns = "tbl_post_id"
        ),
        @ForeignKey(
            entity = TblCategory.class,
            parentColumns = "cat_id",
            childColumns = "tbl_category_id"
        )
    })
    class TblPostCategory {
        @PrimaryKey
        @ColumnInfo(name="tbl_post_id")
        public String id;
    
        @ColumnInfo(name = "tbl_category_id")
        public String categoryId;
    }
    

提交回复
热议问题