英文:
Android Room one to many relationship
问题
我试图创建一个一对多的关系,其中每个user
实例对应于零个或多个role
实例。
问题: UserRoleJunction
在@Relation
中的parentColumn
(user)和entityColumn
(role)存在问题。
由于错误,我还没有达到DAO
的实现。
user
+----+----------+----------+
| id | username | name |
+----+----------+----------+
| 1 | johndoe | John Doe |
| 2 | janedoe | Jane Doe |
+----+----------+----------+
data class
@Entity(tableName = "user")
data class User(
@PrimaryKey(autoGenerate = true)
var id: Long,
var username: String? = null,
var name: String? = null)
role
+----+----------+
| id | name |
+----+----------+
| 1 | Sales |
| 2 | Shipping |
| 3 | Accounts |
+----+----------+
data class
@Entity(tableName = "role")
data class Role(
@PrimaryKey(autoGenerate = true)
var id: Long,
var name: String? = null)
user_role 关联表
+---------+---------+
| user_id | role_id |
+---------+---------+
| 1 | 1 |
| 1 | 3 |
| 2 | 1 |
+---------+---------+
data class
@Entity(tableName = "user_role", primaryKeys = ["user_id", "role_id"])
data class UserRoleJoin( // 关联表
@ColumnInfo(name = "user_id") var userId: Int,
@ColumnInfo(name = "role_id") var roleId: Int)
Junction 数据类
data class UserRoleJunction (
@Embedded var user: User,
@Relation(
parentColumn = "id", // User -> 错误:见下文
entityColumn = "id", // Role -> 错误:见下文
associateBy = Junction(UserRoleJoin::class)
)
var roles: List<Role>
)
错误 1 UserRoleJunction
parentColumn
错误:在关联表
UserRoleJoin
中找不到父实体引用列id
。选项:user_id
,role_id
entityColumn
错误:在关联表
UserRoleJoin
中找不到子实体引用列id
。选项:user_id
,role_id
我尝试根据错误消息替换user_id
和role_id
,但它继续抛出类似上面的错误。
错误 2 UserRoleJunction
实体和POJO必须有一个可用的公共构造函数。您可以拥有一个空构造函数或一个构造函数,其参数与字段(按名称和类型)匹配。
英文:
I'm attempting to create a one-to-many relationship where each instance of user
corresponds to zero or more instances of role
entity.
Problem: UserRoleJunction
is having issues in the @Relation
for both parentColumn
(user) and entityColumn
(role)
I didn't reach to DAO
implementation yet because of the error.
user
+----+----------+----------+
| id | username | name |
+----+----------+----------+
| 1 | johndoe | John Doe |
| 2 | janedoe | Jane Doe |
+----+----------+----------+
data class
@Entity(tableName = "user")
data class User(
@PrimaryKey (autoGenerate = true)
var id: Long,
var username: String? = null,
var name: String? = null)
role
+----+----------+
| id | name |
+----+----------+
| 1 | Sales |
| 2 | Shipping |
| 3 | Accounts |
+----+----------+
data class
@Entity(tableName = "role")
data class Role(
@PrimaryKey(autoGenerate = true)
var id: Long,
var name: String? = null)
user_role Join or CrossRef Table
+---------+---------+
| user_id | role_id |
+---------+---------+
| 1 | 1 |
| 1 | 3 |
| 2 | 1 |
+---------+---------+
data class
@Entity(tableName = "user_role", primaryKeys = ["user_id", "role_id"])
data class UserRoleJoin( // CrossRef
@ColumnInfo(name = "user_id") var userId: Int,
@ColumnInfo(name = "role_id") var roleId: Int)
Junction data class
data class UserRoleJunction (
@Embedded var user: User,
@Relation(
parentColumn = "id", // User -> error: see below
entityColumn = "id", // Role -> error: see below
associateBy = Junction(UserRoleJoin::class)
)
var roles: List<Role>
)
Error 1 UserRoleJunction
parentColumn
> error: Cannot find the parent entity referencing column id
in the junction UserRoleJoin
. Options: user_id
, role_id
entityColumn
> error: Cannot find the child entity referencing column id
in the junction UserRoleJoin
. Options: user_id
, role_id
I did try substituting user_id
and role_id
as per the error messages but it keeps throwing similar errors like above.
Error 2 UserRoleJunction
> Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
答案1
得分: 0
在使用关联/联接时,涉及4个列。
两个父级和两个子级。@Relation
层次的 parentColumn
和 entityColumn
定义了 @Relation
(角色)表和 @Embedded
(用户)表中的列。
Junction
用于指定联接表中的相应列,这些列已被省略,并且是错误的原因。
因此,您应该使用:
Junction(UserRoleJoin::class, parentColumn = "user_id", entityColumn = "role_id")
英文:
When using a association/junction there are 4 columns involved.
Two parents and two children. The parentColumn and entityColumn at the @Relation
level define the columns in the @Relation
(Role) and the @Embedded
(User) tables.
The Junction
is used to specify the respective columns in the junction table itself, these have been omitted and are the cause of the errors.
So you should be using:-
Junction(UserRoleJoin::class, parentColumn = "user_id", entityColumn = "role_id")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论