【发布时间】: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