gpt4 book ai didi

android - 如何使用 Android Room 创建一个包含两个或多个外键的表?

转载 作者:IT王子 更新时间:2023-10-29 06:22:04 25 4
gpt4 key购买 nike

enter image description here

根据entity-relationship model , tbl_posttbl_category 之间的关系可以使用 Room Persistency Library 指定如下:

@Entity(foreignKeys = @ForeignKey(
entity = TblPost.class,
parentColumns = "id",
childColumns = "tbl_post_id")
)
class TblPostCategory {
@PrimaryKey
public String id;

@ColumnInfo(name = "user_id")
public String postId;
}

但是 TblPostCategory 依赖于两个外键:post_idcategory_id 来自 TblPostTbCategory.

应该如何使用 Room 注释来描述关系?

最佳答案

TblCategory.java

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

@ColumnInfo(name = "cat_name")
public String name;
}

TblPost.java(它缺少外键引用,但对案例来说并不重要)

@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;
}

关于android - 如何使用 Android Room 创建一个包含两个或多个外键的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45988446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com