android Room多对多关系只返回连接查询结果的第一条记录

问题描述

我有两个具有 M:N 关系的实体,并根据 documentation 定义关系类。 我的课程结构:

Contcat.kt

@Entity(tableName = "contacts")
class Contact{

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "contact_id")
    var contactId: Int = 0

    @ColumnInfo(name = "user_mobile_no")
    var userMobileNumber: String = ""

    @ColumnInfo(name = "name")
    var name: String = ""

    @Ignore
    constructor()

    constructor(
        userMobileNumber: String,name: String
    ) {
        this.userMobileNumber = userMobileNumber
        this.name = name
    }

}

组.kt

@Entity(tableName = "groups")
class Group{

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "group_id")
    var groupId: Int = 0

    @ColumnInfo(name = "name")
    var name: String = ""


    @Ignore
    constructor()


    constructor(
        name: String
    ) {
        this.name = name
    }   

}

ContactWithGroups.kt

    class ContactWithGroups {
        @Embedded
        lateinit var contact: Contact
    
        @Relation(
            parentColumn = "contact_id",entityColumn = "group_id",entity = Group::class,associateBy = Junction(
                ContactGroupCrossRef::class,parentColumn = "contact_id",entityColumn = "group_id"
            )
        )
        lateinit var groups: List<Group>
    }

ContactGroupCrossRef.kt

@Entity(primaryKeys = ["contact_id","group_id"],foreignKeys = [
        ForeignKey(
            entity = Contact::class,parentColumns = ["contact_id"],childColumns = ["contact_id"]
        ),ForeignKey(
            entity = Group::class,parentColumns = ["group_id"],childColumns = ["group_id"]
        )
    ]
)
data class ContactGroupCrossRef (
    var contact_id: Int,var group_id: Int
)

ContactDao.kt

@Dao
            interface ContactDao{
            //...
            @Transaction
                @Query("SELECT * FROM contacts WHERE contact_id= :contactId")
                fun getContactWithGroupsById(contactId: Int): List<ContactWithGroups>
            }

当我运行 getContactWithGroupsById() 时,它返回联系人及其第一个组,而在数据库中记录存在并正确插入。例如,如果 contact_id=1 ,并且此联系人具有三个组,该记录存在于 ContactGroupCrossRef 表中(例如 group_ids = 1,2,3 for contact_id=1)仅联系人和 group_id=1 返回此联系人!

我的错误是什么?我的实体构造函数可能有问题?!我需要这个实体是自动生成的主键。提前感谢您帮助我发现错误。

PS: 1) 我为 Room 使用了以下依赖项:

def room_version = "2.2.5"

implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

implementation "androidx.room:room-ktx:$room_version"
kapt 'android.arch.persistence.room:compiler:1.1.1'

def room_rx_version = "1.0.0"
implementation "android.arch.persistence.room:rxjava2:$room_rx_version"
  1. 无关,但要了解更多详细信息:我在 rxjava Observable 和 kotlin 协程中都调用了 getContactWithGroupsById()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...