java—如何使用带有fk的表通过查询室获取完整的实体对象

i2byvkas  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(279)

假设我有这些table:

表-用户

存储用户

@Entity(
    tableName = "USER"
)
data class User(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "user_id")
    val id: Int,

    @ColumnInfo(name = "user_name")
    val name: String
)

表-项目

储存物品就像一个产品

@Entity(
    tableName = "ITEM"
)
data class Item(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "item_id")
    val id: Int,

    @ColumnInfo(name = "item_name")
    val name: String,

    @ColumnInfo(name = "item_description")
    val description: String
)

表-特殊

存储产品1的特殊需要产品存在

@Entity(
    tableName = "SPECIAL",
    foreignKeys = [ForeignKey(
        entity = Item::class,
        parentColumns = ["item_id"],
        childColumns = ["special_item_id"]
    )]
)
data class Special(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "special_id")
    val id: Int,

    @ColumnInfo(name = "special_item_id")
    val coupon_product_id: Int,

    @ColumnInfo(name = "special_name")
    val name: String,

    @ColumnInfo(name = "special_description")
    val description: String

)

表--收藏夹

存储用户最喜爱的特价商品

@Entity(
    tableName = "TB_FAVOURITE",
    foreignKeys = [ForeignKey(
        entity = User::class,
        parentColumns = ["user_id"],
        childColumns = ["favourite_user_id"]
    ), ForeignKey(
        entity = Special::class,
        parentColumns = ["special_id"],
        childColumns = ["favourite_special_id"]
    )]
)
data class Favourite(
    @PrimaryKey
    @ColumnInfo(name = "favourite_user_id")
    val id: Int,

    @ColumnInfo(name = "favourite_special_id")
    val specialId: Int

)

我的问题是,如何查询选择所有的特价商品,然后创建一个类来存储用户是否喜欢的商品。目前是单用户应用,是演示应用。所以,用户总是一样的,所以我可以硬编码 findById 并发送用户的id。

目标

是以列表形式获取查询结果,其中包含:
所有的特色菜
在someclass中应该包含
一个标志,知道它是否是司机的最爱
问题是我希望能够将房间数据库的结果Map到我想要的对象,所以我猜查询比Map器更重要,我知道如何做Map器。
注意我用的是 assets/database/mydb.db 我不知道这是否重要。
查询将如何做到这一点?db的结构是否有任何改进以使其更容易?

bvhaajcl

bvhaajcl1#

您可以创建如下类:

class SpecialWithFavourite {
    @Embedded
    var special: Special? = null
    @Relation(parentColumn = "special_item_id", entityColumn = "item_id")
    var item: Item? = null
    @Relation(parentColumn = "special_id", entityColumn = "favourite_special_id")
    var favourite: Favourite? = null
}

然后使用dao查询进行提取,如下所示:

@Dao
interface DaoSpecialWithFavourite {
    @Transaction
    @Query("SELECT SPECIAL.* FROM SPECIAL INNER JOIN TB_FAVOURITE WHERE TB_FAVOURITE.favourite_user_id = :userId")
    fun getLD(userId: Long): List<SpecialWithFavourite>
}

另一种选择是使用视图,特别是当您只需要五个字段时

@DatabaseView("SELECT SPECIAL.*, ITEM.item_name FROM SPECIAL INNER JOIN ITEM ON ITEM.item_id = SPECIAL.special_item_id")
class FavSpecial {
    var name: String? = null
    @Embedded
    var special: Special? = null
}

根据评论中的讨论,新的 Favourite ```
data class Favourite(
@PrimaryKey(autoGenerate = true)
val id: Int,
@ColumnInfo(name = "favourite_user_id")
val userid: Int,
@ColumnInfo(name = "favourite_special_id")
val specialId: Int
)

这样,您仍然可以保持约束,用户可以有多个喜爱的特价

相关问题