【问题标题】:kotlin android room ForeignKeykotlin android 房间外键
【发布时间】:2020-08-29 09:35:18
【问题描述】:

我有两个实体 A 和 B,其中一行是 id ,接下来我有实体 Test 它看起来像:

@Parcelize
@Entity(
    tableName = "social_group_discount_relations", primaryKeys = ["oneID", "twoId"],
    foreignKeys = [
        ForeignKey(
            entity = A::class,
            parentColumns = ["id"],
            childColumns = ["oneID"],
            onDelete = CASCADE
        ), ForeignKey(
            entity = B::class,
            parentColumns = ["id"],
            childColumns = ["twoId"],
            onDelete = CASCADE
        )
    ]
)
data class Test(
    @SerializedName("oneID") val oneID: Long,
    @SerializedName("twoId") val twoId: Long
) : Parcelable

接下来我想试试 get 和 obcject :

日期类 AB (@Embedded val a: A, @Embedded val b: B)@Parcelize @实体( tableName = "折扣" )

data class Discount(
    @PrimaryKey @SerializedName("id") val id: Long,
    @SerializedName("description") val description: String?,
    @SerializedName("name") val name: String?,
    @SerializedName("rate") val rate: Int?,
    @SerializedName("operator") val operatorId: Long?

) : Parcelable

@Parcelize
@Entity(tableName = "social_group_discount")
data class SocialGroupDiscount(
    @PrimaryKey @SerializedName("id") val id: Long,
    @SerializedName("description") val description: String?,
    @SerializedName("name") val name: String?,
    @SerializedName("code") val code: String?,
    @SerializedName("operator") val operator: Long?

) : Parcelable

@Parcelize
data class SocialGroupAndDiscount(
    @Embedded(prefix = "first_") val discount: Discount,
    @Embedded(prefix = "second_") val socialGroupDiscountRelations: SocialGroupDiscountRelations

) : Parcelable


@Parcelize
@Entity(
    tableName = "social_group_discount_relations", primaryKeys = ["socialGroupId", "discountId"],
    indices = [
        Index(value = ["socialGroupId"]),
        Index(value = ["discountId"])
    ],
    foreignKeys = [
        ForeignKey(
            entity = Discount::class,
            parentColumns = ["id"],
            childColumns = ["discountId"],
            onDelete = CASCADE
        ), ForeignKey(
            entity = SocialGroupDiscount::class,
            parentColumns = ["id"],
            childColumns = ["socialGroupId"],
            onDelete = CASCADE
        )
    ]
)
open class SocialGroupDiscountRelations(
    @SerializedName("socialgroupid") val socialGroupId: Long,
    @SerializedName("discountid") val discountId: Long
) : Parcelable

这是我的查询:

@Query(
        "SELECT discount.id as first_id,discount.name as first_name, discount.rate as first_rate, discount.operatorId as first_operator, discount.description as first_description " +
                " social_group_discount.id as second_id, social_group_discount.name as second_name, social_group_discount.code as second_code, social_group_discount.description as second_description," +
                "social_group_discount.operator as second_operator FROM social_group_discount_relations as AB" +
                " INNER JOIN discount on discount.id=AB.discountId INNER JOIN social_group_discount on social_group_discount.id=AB.socialgroupid"
    )
    fun getAB(): List<SocialGroupAndDiscount>

我在三等舱有一个班级折扣和 socialGroupDiscount,我得到了 List ids discountId 和 groupId,接下来我想创建将这个 ids 映射到折扣和团体折扣的对象

【问题讨论】:

标签: android android-room


【解决方案1】:

您可以尝试查找many-to-many relationships,但我不确定您是否可以仅使用 Room 工具获得所需的内容。

相反,您可以对 sqlite-query 使用不那么干净的决策:

  1. 在嵌入对象的字段中使用前缀以确保结果表中的字段不会混合:

    data class AB (@Embedded(prefix = "first_") val a: A, @Embedded(prefix = "second_") val b: B )

  2. 在您的 DAO 中,对所有需要的表进行“手动”查询,但在这种情况下,您必须获取实体 A 和 B 的所有字段并为它们添加前缀:

    @Query(" SELECT A.id as first_id,A.someField as first_someField, B.id as second_id, B.anotherField as second_anotherField FROM social_group_discount_relations as AB INNER JOIN A on A.id=AB.oneID INNER JOIN B on B.id=AB.twoId ") fun getAB(): List<AB>

更新示例

假设您有带字段的 A 类:

  • 身份证
  • 姓名

还有你的 B 类结构:

  • 身份证
  • 地址
  • 数量

那么你的数据类 AB 应该是:

data class AB (@Embedded(prefix = "first_") val a: A,
  @Embedded(prefix = "second_") val b: B
)

还有你的 DAO 方法:

@Query("SELECT A.id as first_id,A.name as first_name, 
B.id as second_id, B.address as second_address, B.quantity as second_quantity 
FROM social_group_discount_relations as AB 
INNER JOIN A on A.id=AB.oneID 
INNER JOIN B on B.id=AB.twoId")
fun getAB(): List<AB>

【讨论】:

  • 好吧,也许你可以澄清一下——你有什么错误(那是什么?)或者你的查询没有返回任何东西?您是否根据您的 A 类和 B 类结构更改了答案中给出的查询,还是“按原样”获得?
  • @Query("select * From social_group_discount_relations , aWhere discountId = a.id ") fun getAB(): List 但我只上了a类我没有b类
  • 我没看懂你写的什么。这与我的回答无关。在我的答案中添加了示例。
  • Sergiy 和我从 dao 类 Test 中调用的这个?
  • 从技术上讲,您可以将它放在任何 dao 类中。这只是一个功能。您甚至可以对所有实体都只有一个 dao。但是如果你有 dao 类测试 - 所以把它放在那里。
猜你喜欢
  • 2022-01-24
  • 2022-07-14
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多