在房间数据库中处理这种关系的最佳方式
Best way to handle this relation in Room Database
我有三个 table,我在这里有一个多对多的关系:
联系人table:
@Entity(tableName = "T_Contacts")
class Contact(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "_id") var id: Long,
@ColumnInfo(name = "phoneNumber")
var phoneNumber: String = "",
@ColumnInfo(name = "name")
var name: String = "",
@ColumnInfo(name = "active")
var active: Int = 1
@ColumnInfo(name = "contactId")
var contactId: Long = -1
@ColumnInfo(name = "phoneContactId")
var phoneContactId: String = ""
}
电子邮件 table:
@Entity(tableName = "T_EmailAddress")
class EmailAddress(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "_id")
var id : Long,
@ColumnInfo(name="emailAddress")
var emailAddress: String,
@ColumnInfo(name="active")
var active : Int) {
}
和联想table:
@Entity(tableName = "T_EmailAddressContact",
foreignKeys = [ForeignKey(
entity = Contact::class,
parentColumns = arrayOf("contactId"),
childColumns = arrayOf("contactId"),
onDelete=ForeignKey.CASCADE),
ForeignKey(
entity = EmailAddress::class,
parentColumns = arrayOf("emailId"),
childColumns = arrayOf("emailId"),
onDelete=ForeignKey.CASCADE)]
)
class EmailAddressContactCrossRef(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
var id: Long,
@ColumnInfo(name="contactId")
var contactId: Long,
@ColumnInfo(name="emailId")
var emailId : Long,
@ColumnInfo(name="active")
var active : Int ) {
如何使用关系获取所有电子邮件联系人的列表?我试过了:
data class EmailAddressContact(
@Embedded val contact: EmailAddress,
@Relation(parentColumn = "contactId",
entityColumn = "contactId",
associateBy = Junction(EmailAddressContactCrossRef::class)
)
val listContacts: List<Contact>
)
但这只会让我得到一个特定电子邮件地址的所有联系人列表。我想要这样的东西:
data class EmailAddressContact {
val id: Long,
val emailAddress: EmailAddress,
val contact: Contact,
val active: Boolean
}
通过调用查询:
@Transaction
@Query("SELECT * FROM T_EmailAddressContact")
fun getAllEmailAddressAndContacts(): List<EmailAddressContact>
根据您的 EmailAddressContact
结构,您似乎不需要使用联结,只需使用关系。试试这个方法:
data class EmailAddressContact(
@Embedded val emailAddressContactCrossRef: EmailAddressContactCrossRef, // <- there you'll get "id" and "active" fields
@Relation(
parentColumn = "contactId",
entityColumn = "contactId"
)
val contact: Contact,
@Relation(
parentColumn = "emailId",
entityColumn = "_id"
)
val emailAddress: EmailAddress
)
你在问题中提到的查询应该适合
我有三个 table,我在这里有一个多对多的关系:
联系人table:
@Entity(tableName = "T_Contacts")
class Contact(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "_id") var id: Long,
@ColumnInfo(name = "phoneNumber")
var phoneNumber: String = "",
@ColumnInfo(name = "name")
var name: String = "",
@ColumnInfo(name = "active")
var active: Int = 1
@ColumnInfo(name = "contactId")
var contactId: Long = -1
@ColumnInfo(name = "phoneContactId")
var phoneContactId: String = ""
}
电子邮件 table:
@Entity(tableName = "T_EmailAddress")
class EmailAddress(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "_id")
var id : Long,
@ColumnInfo(name="emailAddress")
var emailAddress: String,
@ColumnInfo(name="active")
var active : Int) {
}
和联想table:
@Entity(tableName = "T_EmailAddressContact",
foreignKeys = [ForeignKey(
entity = Contact::class,
parentColumns = arrayOf("contactId"),
childColumns = arrayOf("contactId"),
onDelete=ForeignKey.CASCADE),
ForeignKey(
entity = EmailAddress::class,
parentColumns = arrayOf("emailId"),
childColumns = arrayOf("emailId"),
onDelete=ForeignKey.CASCADE)]
)
class EmailAddressContactCrossRef(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
var id: Long,
@ColumnInfo(name="contactId")
var contactId: Long,
@ColumnInfo(name="emailId")
var emailId : Long,
@ColumnInfo(name="active")
var active : Int ) {
如何使用关系获取所有电子邮件联系人的列表?我试过了:
data class EmailAddressContact(
@Embedded val contact: EmailAddress,
@Relation(parentColumn = "contactId",
entityColumn = "contactId",
associateBy = Junction(EmailAddressContactCrossRef::class)
)
val listContacts: List<Contact>
)
但这只会让我得到一个特定电子邮件地址的所有联系人列表。我想要这样的东西:
data class EmailAddressContact {
val id: Long,
val emailAddress: EmailAddress,
val contact: Contact,
val active: Boolean
}
通过调用查询:
@Transaction
@Query("SELECT * FROM T_EmailAddressContact")
fun getAllEmailAddressAndContacts(): List<EmailAddressContact>
根据您的 EmailAddressContact
结构,您似乎不需要使用联结,只需使用关系。试试这个方法:
data class EmailAddressContact(
@Embedded val emailAddressContactCrossRef: EmailAddressContactCrossRef, // <- there you'll get "id" and "active" fields
@Relation(
parentColumn = "contactId",
entityColumn = "contactId"
)
val contact: Contact,
@Relation(
parentColumn = "emailId",
entityColumn = "_id"
)
val emailAddress: EmailAddress
)
你在问题中提到的查询应该适合