Android Room一对多关系

huangapple go评论50阅读模式
英文:

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_idrole_id

entityColumn

错误:在关联表UserRoleJoin中找不到子实体引用列id。选项:user_idrole_id

我尝试根据错误消息替换user_idrole_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 = &quot;user&quot;)
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 = &quot;role&quot;)
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 = &quot;user_role&quot;, primaryKeys = [&quot;user_id&quot;, &quot;role_id&quot;])
data class UserRoleJoin( // CrossRef
    @ColumnInfo(name = &quot;user_id&quot;) var userId: Int,
    @ColumnInfo(name = &quot;role_id&quot;) var roleId: Int)

Junction data class

data class UserRoleJunction (
    @Embedded var user: User,
    @Relation(
        parentColumn = &quot;id&quot;, // User -&gt; error: see below
        entityColumn = &quot;id&quot;, // Role -&gt; error: see below
        associateBy = Junction(UserRoleJoin::class)
    )
    var roles: List&lt;Role&gt;
)

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 层次的 parentColumnentityColumn 定义了 @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 = &quot;user_id&quot;, entityColumn = &quot;role_id&quot;)

huangapple
  • 本文由 发表于 2023年2月6日 02:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定