房间@Relation注释,一对一关系
我想从数据库中获得POJO,该数据库中有Pojo作为属性的列表。 根据文档的说法,这是通过@Relationship注释可行的。但是,这是我没有直接引用所讨论的表/实体的一项关系。 我该如何直接从DAO中从数据库中恢复原状?这是甚至可能的,还是我必须手动实现一些中介绑定逻辑? 我想从DB获得POJO:
data class Chore(
var name: String,
//This is the line that doesn't work
@Relation(parentColumn = "id", entityColumn = "userId", entity = UserEntity::class)
var contributingUsers: List<User>,
@DrawableRes var drawableRes: Int,
var done: Boolean
)
我想自动映射的用户POJO:
data class User(
val userName: String,
@DrawableRes val userPhoto: Int
)
一对多的参考表/实体:
@Entity(
tableName = TableNames.CHORE_TO_USER,
foreignKeys = [
ForeignKey(
entity = UserEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("userId"),
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE,
),
ForeignKey(
entity = ChoreEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("choreId"),
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE,
)
],
indices = [Index("choreId"), Index("userId")]
)
internal data class ChoreToUser(
val userId: Int,
val choreId: Int,
val canFulfill: Boolean,
): BaseEntity()
查询:
@Query("SELECT Chores.name, drawableRes," +
"Users.name as userName, Users.imageRes as userPhoto, " +
"(COUNT(ChoreLogs.id) = 1) as done " +
"FROM Chores " +
"LEFT JOIN ChoreToUsers ON ChoreToUsers.choreId = Chores.id " +
"LEFT JOIN Users ON ChoreToUsers.userId = Users.id " +
"LEFT JOIN ChoreLogs ON ChoreLogs.choreToUserId = ChoreToUsers.id")
fun getChoreTiles(): List<Chore>
TLDR: 我想将用户列表嵌入到繁琐的pojo中。它是通过中介表重新的。我该怎么做?
I have a POJO I'd like to get from the database which has a list of POJO's as a property.
This, according to docs is doable via a @Relationship annotation. However, it's a one to many relationship where I don't directly reference the table/entity in question.
How would I go about getting this back from the DB directly from the DAO? Is this even possible, or do I have to implement some intermediary binding logic manually?
The POJO I'd like to get from DB:
data class Chore(
var name: String,
//This is the line that doesn't work
@Relation(parentColumn = "id", entityColumn = "userId", entity = UserEntity::class)
var contributingUsers: List<User>,
@DrawableRes var drawableRes: Int,
var done: Boolean
)
The User POJO I'd like to get automatically mapped:
data class User(
val userName: String,
@DrawableRes val userPhoto: Int
)
The One-To-Many reference table/entity:
@Entity(
tableName = TableNames.CHORE_TO_USER,
foreignKeys = [
ForeignKey(
entity = UserEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("userId"),
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE,
),
ForeignKey(
entity = ChoreEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("choreId"),
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE,
)
],
indices = [Index("choreId"), Index("userId")]
)
internal data class ChoreToUser(
val userId: Int,
val choreId: Int,
val canFulfill: Boolean,
): BaseEntity()
The query:
@Query("SELECT Chores.name, drawableRes," +
"Users.name as userName, Users.imageRes as userPhoto, " +
"(COUNT(ChoreLogs.id) = 1) as done " +
"FROM Chores " +
"LEFT JOIN ChoreToUsers ON ChoreToUsers.choreId = Chores.id " +
"LEFT JOIN Users ON ChoreToUsers.userId = Users.id " +
"LEFT JOIN ChoreLogs ON ChoreLogs.choreToUserId = ChoreToUsers.id")
fun getChoreTiles(): List<Chore>
TLDR:
I wanna embed a list of users into the Chore POJO. It's refferenced via an intermediary table. How would I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用
@RELATION
必须可用,以便找到父列。因此,您将需要以下内容: -
简而需要该父母。请注意,这些是为了通过便利方法使用,这些方法本质上有些限制。
但是,由于您有一个中间表(映射/关联/引用....表),因此您需要通过使用AsseciateBy参数来告诉房间,以定义 innction
@RELATION
将构建基础查询,以相应地访问孩子。如果您想要您的查询的结果,那么繁琐的对象可能只是: -
,但对于每个与杂务相关的用户的组合都存在将有一排,即结果是笛卡尔产品。
如果您想用它的子用户列表建立
工作示例
基于 userentity 是: -
和 userentity 是: -
and choretouser是: -
示例1-来自您的笛卡尔产品查询,
因此使用相当于繁琐的类型 class noce note chore1 : -
上述使用的DAO函数是: -
注意,为方便/简洁,脉络脉连接已被排除在外
请参见Ex01
的结果
示例2-使用选项a)
此处使用 chore2 已被用作结果类别,它为: -
与2个查询一起使用的列表,并且使用查询的函数,这些列表是: -
再次不需要 @relation,因为查询可以完成所有需要的内容。
。
请参阅Ex02
的结果
示例3-使用选项b),但通过room
ie使用
@embedded
,@RELATION
并且由于有中间的关联表jectiateby
和innction
。在这种情况下,等效类是 chore3 : -
DAO函数为: -
请 这3个示例
在活动中包括以下代码(为方便/简洁,在主线程上运行): -
和
结果(aka输出中包含在日志中,每个示例拆分)
To use
@Relation
the parent table must be available so that the parent column can be found.So you would need something along the lines of :-
In short
@Relation
results in a sub query being invoked that retrieves ALL of the children of the parent (which MUST exist/be known) thus an @Embed of that parent is required. Note that these are for use by the convenience methods, which are a little restrictive in nature.However, as you have an intermediate table (mapping/associative/reference .... table) then you need to tell Room about this by using the associateBy parameter to define the Junction
@Relation
will build the underlying query to access the children accordingly.If you want the result of your query then a Chore object could just be:-
BUT a row would exist for every combination that is for every User that is related to a Chore there would be a row i.e. the result is the cartesian product.
If you wanted to build a Chore with a list of it's child Users then you would have to
Working Examples
Based upon UserEntity being :-
and UserEntity being :-
and ChoreToUser being:-
Example 1 - Cartesian Product from you Query
So using the sort of equivalent of your Chore class there is Chore1 :-
The dao function used with the above was:-
Note for convenience/brevity the ChoreLogs JOIN has been excluded
See results for EX01
Example 2 - Using the option a)
Here Chore2 has been used as the resultant class, it being:-
This used in conjunction with 2 queries and a function that uses the queries, these being:-
again no need for an @Relation as the queries do all that is required.
See results for EX02
Example 3 - using option b) BUT via Room
i.e. using
@Embedded
, with@Relation
AND as there is the intermediate associative tableassociateBy
and theJunction
.In this case the equivalent class is Chore3 :-
The Dao function being:-
Testing/Demonstrating the 3 Examples
The following code was included in an activity (run on the main thread for convenience/brevity):-
and
Results (aka output included in the log, split per example)