Kotmin Room crossRef 具有相同名称的键的实体
我正在创建一个 CRUD 工厂。基本上我所有的实体都将从 BaseEntity 继承,并以 id
作为主键 我试图了解如何为 M2M 关系创建交叉引用表。 这是一个简化的示例,没有继承。 ArticlesEntity
有许多 MagasinsEntity
,而 MagasinsEntity
有许多 ArticlesEntity
。实体 ArticlesMagasinsCrossRef
是连接点
,但 ArticlesEntity 和 MagasinsEntity 都将 id
作为primaryKey。
@Entity(
tableName = "articles"
)
data class ArticlesEntity(
@PrimaryKey(autoGenerate = false) val id: UUID = UUID.randomUUID(),
val title: String,
)
@Entity(tableName = "magasins")
data class MagasinsEntity(
@PrimaryKey(autoGenerate = false) val id: UUID = UUID.randomUUID(),
val nomMagasin: String
)
@Entity(
tableName = "articles_magasins"
)
data class ArticlesMagasinsCrossRefEntity(
val id: UUID, // how is it possible here to have the id of Articles ?
val id: UUID // how is it possible here to have the id of Magasins ?
)
编辑 我当然尝试更改列的名称:
data class ArticlesMagasinsCrossRefEntity(
val articleRd: UUID
val magasinId: UUID
)
但是关系数据类的构建失败:例如
data class RelMagasinWithArticles(
@Embedded val magasin: MagasinsEntity,
@Relation(
parentColumn = "magasinId",
entityColumn = "id",
associateBy = Junction(ArticlesMagasinsCrossRefEntity::class)
)
val articles: List<ArticleEntity>
)
I'm creating a CRUD factory. Basically all my entites will inherit from a BaseEntity with id
as a primary key
I'm trying to understand how to create a cross ref table for a M2M relationship.
Here is a simplifyied example, without inheritance. ArticlesEntity
have many MagasinsEntity
and MagasinsEntity
many ArticlesEntity
. The Entity ArticlesMagasinsCrossRef
is the junction
But both ArticlesEntity and MagasinsEntity have id
as the primaryKey.
@Entity(
tableName = "articles"
)
data class ArticlesEntity(
@PrimaryKey(autoGenerate = false) val id: UUID = UUID.randomUUID(),
val title: String,
)
@Entity(tableName = "magasins")
data class MagasinsEntity(
@PrimaryKey(autoGenerate = false) val id: UUID = UUID.randomUUID(),
val nomMagasin: String
)
@Entity(
tableName = "articles_magasins"
)
data class ArticlesMagasinsCrossRefEntity(
val id: UUID, // how is it possible here to have the id of Articles ?
val id: UUID // how is it possible here to have the id of Magasins ?
)
Edit
I tried of course to change the name of the columns:
data class ArticlesMagasinsCrossRefEntity(
val articleRd: UUID
val magasinId: UUID
)
but the build failed for the relation data class :for example
data class RelMagasinWithArticles(
@Embedded val magasin: MagasinsEntity,
@Relation(
parentColumn = "magasinId",
entityColumn = "id",
associateBy = Junction(ArticlesMagasinsCrossRefEntity::class)
)
val articles: List<ArticleEntity>
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用Junction的parentColumn和entityColumn参数,例如
注意
您还需要定义一个主要ArticlesMagasinsCrossRefEntity 类的键,例如:-
演示
因此,使用您的代码与建议的代码覆盖您的代码并使用以下 @Dao 接口:-
活动中合适的 @Database 注释类和代码:-
日志的输出是:-
You need to use the Junction's parentColumn and entityColumn parameters e.g.
Note
You will also need to define a primary key for the ArticlesMagasinsCrossRefEntity class e.g. :-
Demonstration
So using you code with the suggested code overriding your code and with the following @Dao interface:-
a suitable @Database annotated class and code in the activity:-
The output to the log is:-