房间@Relation注释,一对一关系

发布于 2025-02-09 14:09:12 字数 1927 浏览 1 评论 0原文

我想从数据库中获得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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半岛未凉 2025-02-16 14:09:13

要使用@RELATION 必须可用,以便找到父列。

因此,您将需要以下内容: -

data class Chore(
    @Embedded
    var choreEntity: ChoreEntity, //<<<<<
    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
)

简而需要该父母。请注意,这些是为了通过便利方法使用,这些方法本质上有些限制。

但是,由于您有一个中间表(映射/关联/引用....表),因此您需要通过使用AsseciateBy参数来告诉房间,以定义 innction

  • 不需要这样的桌子来进行一对多关系,这些表可以是使用但实际上是为了多种关系。

@RELATION将构建基础查询,以相应地访问孩子。

如果您想要您的查询的结果,那么繁琐的对象可能只是: -

data class Chore(
    var name: String,
    var contributingUsers,
    @DrawableRes var drawableRes: Int,
    var done: Boolean
)

,但对于每个与杂务相关的用户的组合都存在将有一排,即结果是笛卡尔产品。

如果您想用它的子用户列表建立

  • 琐事
  • 一个 。

工作示例

基于 userentity 是: -

@Entity( tableName = TableNames.CHORE)
data class ChoreEntity(
    @PrimaryKey
    val id: Long?=null,
    val name: String,
    val drawableRes: Int
)

userentity 是: -

@Entity(tableName = TableNames.USER)
data class UserEntity(
    @PrimaryKey
    val id: Long? = null,
    val name: String,
    val imageRes: Int
    /* etc */
)

and choretouser是: -

@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")], // Replaced by required primary key
    primaryKeys = ["choreId","userId"]
)
data class ChoreToUser(
    var userId: Long,
    var choreId: Long,
    @ColumnInfo(index = true)
    var canFulfill: Boolean,
)
  • 请注意,该索引已被 @primary键替换(房间需要一个主钥匙)。它们实际上是相同的。此外,@columninfo还用于在用户ID列(更有效)上创建索引。

示例1-来自您的笛卡尔产品查询

因此使用相当于繁琐的类型 class noce note chore1 : -

data class Chore1(
    var name: String,
    var userName: String, // ADDED for demo
    //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
)
  • @relation无法使用@embedded发表了评论。

上述使用的DAO函数是: -

@Query("SELECT Chores.name, drawableRes," +
        "Users.name as userName, Users.imageRes as userPhoto, " +
        " 10 /*(COUNT(ChoreLogs.id) = 1)*/ as done " + // not using Chorelogs table so fake output
        "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 */") // SQL commented out for Brevity
fun getChoreTiles(): List<Chore1> /* Cartesian Product */
  • 注意,为方便/简洁,脉络脉连接已被排除在外

  • 请参见Ex01

    的结果

示例2-使用选项a)

此处使用 chore2 已被用作结果类别,它为: -

data class Chore2(
    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
)
  • 可以是看到贡献者是

与2个查询一起使用的列表,并且使用查询的函数,这些列表是: -

@Query("SELECT * FROM Chores")
fun getAllChores(): List<ChoreEntity>
@Query("SELECT * FROM  ChoreToUsers JOIN Users ON ChoreToUsers.userId = Users.id WHERE ChoreToUsers.choreId=:choreId")
fun getRelatedUsersForAChore(choreId: Long): List<UserEntity>
@Transaction
@Query("")
fun getRelatedUsersPerChoreAsList(): List<Chore2> {
    var rv = arrayListOf<Chore2>()
    for (ct in getAllChores()) {
        var ul = arrayListOf<User>()
        for (ue in getRelatedUsersForAChore(ct.id!!)) {
            ul.add(User(ue.name,ue.imageRes))
        }
        rv.add(Chore2(ct.name,ul.toList(),ct.drawableRes,false))
    }
    return rv
}
  • 再次不需要 @relation,因为查询可以完成所有需要的内容。

  • 请参阅Ex02

    的结果

示例3-使用选项b),但通过room

ie使用@embedded@RELATION并且由于有中间的关联表jectiatebyinnction

  • IE让房间构建子查询(IES)

在这种情况下,等效类是 chore3 : -

data class Chore3(
    @Embedded
    val chore: ChoreEntity,
    @Relation(
        entity = UserEntity::class,
        parentColumn = "id",
        entityColumn = "id",
        associateBy = Junction(
            value = ChoreToUser::class, parentColumn = "choreId", entityColumn = "userId"
        )
    )
    val contributingUsers: List<UserEntity>
)

DAO函数为: -

@Transaction
@Query("SELECT * FROM Chores")
fun getAllChore3s(): List<Chore3>
  • 参阅Ex03

请 这3个示例

在活动中包括以下代码(为方便/简洁,在主线程上运行): -

const val TAG = "DBINFO"
class MainActivity : AppCompatActivity() {
    lateinit var db: TheDatabase
    lateinit var dao: AllDao
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        db = TheDatabase.getInstance(this)
        dao = db.getAllDao()

        val u1Id = dao.insert(UserEntity(name = "User1", imageRes = 1000))
        val u2Id = dao.insert(UserEntity(name = "User2", imageRes = 2000))
        val u3Id = dao.insert(UserEntity(name = "user3", imageRes = 3000))
        val u4Id = dao.insert(UserEntity(name = "user4", imageRes = 4000))

        val c1Id = dao.insert(ChoreEntity(name = "Chore1", drawableRes = 10000))
        val c2Id = dao.insert(ChoreEntity(name = "Chore2",drawableRes = 20000))
        val c3Id = dao.insert(ChoreEntity(name = "Chore3",drawableRes = 30000))
        val c4Id = dao.insert(ChoreEntity(name = "Chore4",drawableRes = 40000))
        val c5Id = dao.insert(ChoreEntity(name = "Chore5",drawableRes = 50000))
        val c6Id = dao.insert(ChoreEntity(name = "Chore6",drawableRes = 60000))

        /* Mapping */

        dao.insert(ChoreToUser(u1Id,c1Id,false))
        dao.insert(ChoreToUser(u1Id,c2Id,true))
        dao.insert(ChoreToUser(u1Id,c3Id,false))
        dao.insert(ChoreToUser(u1Id,c4Id,false))

        dao.insert(ChoreToUser(u2Id,c5Id,true))
        dao.insert(ChoreToUser(u2Id,c6Id,true))

        dao.insert(ChoreToUser(u3Id,c1Id,false))
        dao.insert(ChoreToUser(u3Id,c2Id,false))
        dao.insert(ChoreToUser(u3Id,c3Id,false))
        dao.insert(ChoreToUser(u3Id,c4Id,false))
        dao.insert(ChoreToUser(u3Id,c5Id,false))
        dao.insert(ChoreToUser(u3Id,c6Id,false))

        /* EX01 - Cartesain result */
        for (ct in dao.getChoreTiles()) {
            Log.d(TAG+"_EX01","Chore is ${ct.name} + User is ${ct.userName}")
        }

        /* EX02 - using SQl with JOINS */
        for (ct in dao.getRelatedUsersPerChoreAsList()) {
            Log.d(TAG+"EX02","Chore is ${ct.name}, image is ${ct.drawableRes}, there are ${ct.contributingUsers.size}  contributing Users:-" )
            for (u in ct.contributingUsers) {
                Log.d(TAG+"EX02","\tUser is ${u.userName}, photo is ${u.userPhoto}")
            }
        }

        /* EX03 = using @Embedded/@Relation and associateBy/Junction */
        for (c3 in dao.getAllChore3s()) {
            Log.d(TAG+"EX03","Chore is ${c3.chore.name}, image is ${c3.chore.drawableRes}, there are ${c3.contributingUsers.size} contributing users:-")
            for (u in c3.contributingUsers) {
                Log.d(TAG+"EX03","\tUser is ${u.name}, photo is ${u.imageRes}")
            }
        }
    }
}
  • 大多数代码只是加载最终的数据: -

”在此处输入图像描述

  • 请注意,上面的数据利用了一个关联表允许的多种多样。

结果(aka输出中包含在日志中,每个示例拆分)

DBINFO_EX01: Chore is Chore1 + User is User1
DBINFO_EX01: Chore is Chore1 + User is user3
DBINFO_EX01: Chore is Chore2 + User is User1
DBINFO_EX01: Chore is Chore2 + User is user3
DBINFO_EX01: Chore is Chore3 + User is User1
DBINFO_EX01: Chore is Chore3 + User is user3
DBINFO_EX01: Chore is Chore4 + User is User1
DBINFO_EX01: Chore is Chore4 + User is user3
DBINFO_EX01: Chore is Chore5 + User is User2
DBINFO_EX01: Chore is Chore5 + User is user3
DBINFO_EX01: Chore is Chore6 + User is User2
DBINFO_EX01: Chore is Chore6 + User is user3


DBINFOEX02: Chore is Chore1, image is 10000, there are 2  contributing Users:-
DBINFOEX02:     User is User1, photo is 1000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore2, image is 20000, there are 2  contributing Users:-
DBINFOEX02:     User is User1, photo is 1000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore3, image is 30000, there are 2  contributing Users:-
DBINFOEX02:     User is User1, photo is 1000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore4, image is 40000, there are 2  contributing Users:-
DBINFOEX02:     User is User1, photo is 1000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore5, image is 50000, there are 2  contributing Users:-
DBINFOEX02:     User is User2, photo is 2000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore6, image is 60000, there are 2  contributing Users:-
DBINFOEX02:     User is User2, photo is 2000
DBINFOEX02:     User is user3, photo is 3000


DBINFOEX03: Chore is Chore1, image is 10000, there are 2 contributing users:-
DBINFOEX03:     User is User1, photo is 1000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore2, image is 20000, there are 2 contributing users:-
DBINFOEX03:     User is User1, photo is 1000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore3, image is 30000, there are 2 contributing users:-
DBINFOEX03:     User is User1, photo is 1000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore4, image is 40000, there are 2 contributing users:-
DBINFOEX03:     User is User1, photo is 1000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore5, image is 50000, there are 2 contributing users:-
DBINFOEX03:     User is User2, photo is 2000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore6, image is 60000, there are 2 contributing users:-
DBINFOEX03:     User is User2, photo is 2000
DBINFOEX03:     User is user3, photo is 3000
  • ,如EX02和EX03所示产生相同的输出。

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 :-

data class Chore(
    @Embedded
    var choreEntity: ChoreEntity, //<<<<<
    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
)

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

  • there is no need for such a table for one-to-many relationships, such tables can be used but are really for many-many relationships.

@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:-

data class Chore(
    var name: String,
    var contributingUsers,
    @DrawableRes var drawableRes: Int,
    var done: Boolean
)

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

  • a) either process the entire result building the resultant List or
  • b) just extract the respective Chore and then run a query per extracted Chore that returns the List.

Working Examples

Based upon UserEntity being :-

@Entity( tableName = TableNames.CHORE)
data class ChoreEntity(
    @PrimaryKey
    val id: Long?=null,
    val name: String,
    val drawableRes: Int
)

and UserEntity being :-

@Entity(tableName = TableNames.USER)
data class UserEntity(
    @PrimaryKey
    val id: Long? = null,
    val name: String,
    val imageRes: Int
    /* etc */
)

and ChoreToUser being:-

@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")], // Replaced by required primary key
    primaryKeys = ["choreId","userId"]
)
data class ChoreToUser(
    var userId: Long,
    var choreId: Long,
    @ColumnInfo(index = true)
    var canFulfill: Boolean,
)
  • Note that the index has been replaced with @Primary key (Room requires a Primary Key). They are effectively the same. Additionally @ColumnInfo has been used to also create an index on the userId column (more efficient).

Example 1 - Cartesian Product from you Query

So using the sort of equivalent of your Chore class there is Chore1 :-

data class Chore1(
    var name: String,
    var userName: String, // ADDED for demo
    //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
)
  • @Relation cannot be used without an @Embedded so commented out.

The dao function used with the above was:-

@Query("SELECT Chores.name, drawableRes," +
        "Users.name as userName, Users.imageRes as userPhoto, " +
        " 10 /*(COUNT(ChoreLogs.id) = 1)*/ as done " + // not using Chorelogs table so fake output
        "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 */") // SQL commented out for Brevity
fun getChoreTiles(): List<Chore1> /* Cartesian Product */
  • 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:-

data class Chore2(
    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
)
  • As can be seen the contributingUsers is a List

This used in conjunction with 2 queries and a function that uses the queries, these being:-

@Query("SELECT * FROM Chores")
fun getAllChores(): List<ChoreEntity>
@Query("SELECT * FROM  ChoreToUsers JOIN Users ON ChoreToUsers.userId = Users.id WHERE ChoreToUsers.choreId=:choreId")
fun getRelatedUsersForAChore(choreId: Long): List<UserEntity>
@Transaction
@Query("")
fun getRelatedUsersPerChoreAsList(): List<Chore2> {
    var rv = arrayListOf<Chore2>()
    for (ct in getAllChores()) {
        var ul = arrayListOf<User>()
        for (ue in getRelatedUsersForAChore(ct.id!!)) {
            ul.add(User(ue.name,ue.imageRes))
        }
        rv.add(Chore2(ct.name,ul.toList(),ct.drawableRes,false))
    }
    return rv
}
  • 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 table associateBy and the Junction.

  • i.e. letting Room build the sub query(ies)

In this case the equivalent class is Chore3 :-

data class Chore3(
    @Embedded
    val chore: ChoreEntity,
    @Relation(
        entity = UserEntity::class,
        parentColumn = "id",
        entityColumn = "id",
        associateBy = Junction(
            value = ChoreToUser::class, parentColumn = "choreId", entityColumn = "userId"
        )
    )
    val contributingUsers: List<UserEntity>
)

The Dao function being:-

@Transaction
@Query("SELECT * FROM Chores")
fun getAllChore3s(): List<Chore3>
  • See results for EX03

Testing/Demonstrating the 3 Examples

The following code was included in an activity (run on the main thread for convenience/brevity):-

const val TAG = "DBINFO"
class MainActivity : AppCompatActivity() {
    lateinit var db: TheDatabase
    lateinit var dao: AllDao
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        db = TheDatabase.getInstance(this)
        dao = db.getAllDao()

        val u1Id = dao.insert(UserEntity(name = "User1", imageRes = 1000))
        val u2Id = dao.insert(UserEntity(name = "User2", imageRes = 2000))
        val u3Id = dao.insert(UserEntity(name = "user3", imageRes = 3000))
        val u4Id = dao.insert(UserEntity(name = "user4", imageRes = 4000))

        val c1Id = dao.insert(ChoreEntity(name = "Chore1", drawableRes = 10000))
        val c2Id = dao.insert(ChoreEntity(name = "Chore2",drawableRes = 20000))
        val c3Id = dao.insert(ChoreEntity(name = "Chore3",drawableRes = 30000))
        val c4Id = dao.insert(ChoreEntity(name = "Chore4",drawableRes = 40000))
        val c5Id = dao.insert(ChoreEntity(name = "Chore5",drawableRes = 50000))
        val c6Id = dao.insert(ChoreEntity(name = "Chore6",drawableRes = 60000))

        /* Mapping */

        dao.insert(ChoreToUser(u1Id,c1Id,false))
        dao.insert(ChoreToUser(u1Id,c2Id,true))
        dao.insert(ChoreToUser(u1Id,c3Id,false))
        dao.insert(ChoreToUser(u1Id,c4Id,false))

        dao.insert(ChoreToUser(u2Id,c5Id,true))
        dao.insert(ChoreToUser(u2Id,c6Id,true))

        dao.insert(ChoreToUser(u3Id,c1Id,false))
        dao.insert(ChoreToUser(u3Id,c2Id,false))
        dao.insert(ChoreToUser(u3Id,c3Id,false))
        dao.insert(ChoreToUser(u3Id,c4Id,false))
        dao.insert(ChoreToUser(u3Id,c5Id,false))
        dao.insert(ChoreToUser(u3Id,c6Id,false))

        /* EX01 - Cartesain result */
        for (ct in dao.getChoreTiles()) {
            Log.d(TAG+"_EX01","Chore is ${ct.name} + User is ${ct.userName}")
        }

        /* EX02 - using SQl with JOINS */
        for (ct in dao.getRelatedUsersPerChoreAsList()) {
            Log.d(TAG+"EX02","Chore is ${ct.name}, image is ${ct.drawableRes}, there are ${ct.contributingUsers.size}  contributing Users:-" )
            for (u in ct.contributingUsers) {
                Log.d(TAG+"EX02","\tUser is ${u.userName}, photo is ${u.userPhoto}")
            }
        }

        /* EX03 = using @Embedded/@Relation and associateBy/Junction */
        for (c3 in dao.getAllChore3s()) {
            Log.d(TAG+"EX03","Chore is ${c3.chore.name}, image is ${c3.chore.drawableRes}, there are ${c3.contributingUsers.size} contributing users:-")
            for (u in c3.contributingUsers) {
                Log.d(TAG+"EX03","\tUser is ${u.name}, photo is ${u.imageRes}")
            }
        }
    }
}
  • The majority of the code is just loading the data which ends up being:-

enter image description here

enter image description here

and

enter image description here

  • Note that above data takes advantage of the many-many allowable by an associative table.

Results (aka output included in the log, split per example)

DBINFO_EX01: Chore is Chore1 + User is User1
DBINFO_EX01: Chore is Chore1 + User is user3
DBINFO_EX01: Chore is Chore2 + User is User1
DBINFO_EX01: Chore is Chore2 + User is user3
DBINFO_EX01: Chore is Chore3 + User is User1
DBINFO_EX01: Chore is Chore3 + User is user3
DBINFO_EX01: Chore is Chore4 + User is User1
DBINFO_EX01: Chore is Chore4 + User is user3
DBINFO_EX01: Chore is Chore5 + User is User2
DBINFO_EX01: Chore is Chore5 + User is user3
DBINFO_EX01: Chore is Chore6 + User is User2
DBINFO_EX01: Chore is Chore6 + User is user3


DBINFOEX02: Chore is Chore1, image is 10000, there are 2  contributing Users:-
DBINFOEX02:     User is User1, photo is 1000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore2, image is 20000, there are 2  contributing Users:-
DBINFOEX02:     User is User1, photo is 1000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore3, image is 30000, there are 2  contributing Users:-
DBINFOEX02:     User is User1, photo is 1000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore4, image is 40000, there are 2  contributing Users:-
DBINFOEX02:     User is User1, photo is 1000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore5, image is 50000, there are 2  contributing Users:-
DBINFOEX02:     User is User2, photo is 2000
DBINFOEX02:     User is user3, photo is 3000
DBINFOEX02: Chore is Chore6, image is 60000, there are 2  contributing Users:-
DBINFOEX02:     User is User2, photo is 2000
DBINFOEX02:     User is user3, photo is 3000


DBINFOEX03: Chore is Chore1, image is 10000, there are 2 contributing users:-
DBINFOEX03:     User is User1, photo is 1000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore2, image is 20000, there are 2 contributing users:-
DBINFOEX03:     User is User1, photo is 1000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore3, image is 30000, there are 2 contributing users:-
DBINFOEX03:     User is User1, photo is 1000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore4, image is 40000, there are 2 contributing users:-
DBINFOEX03:     User is User1, photo is 1000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore5, image is 50000, there are 2 contributing users:-
DBINFOEX03:     User is User2, photo is 2000
DBINFOEX03:     User is user3, photo is 3000
DBINFOEX03: Chore is Chore6, image is 60000, there are 2 contributing users:-
DBINFOEX03:     User is User2, photo is 2000
DBINFOEX03:     User is user3, photo is 3000
  • as can be seen EX02 and EX03 produce the same output.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文