微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 房间 – 比较实体中的列表

我有房间问题.
我有一个实体:

@Entity(tableName = "Entity")
data class Entity(val recipients: List<ID>?) {
    @ColumnInfo(name = "id")
    @PrimaryKey(autoGenerate = true) var id: Long = 0
}

如您所见,它包含ID列表(typealias ID = Long)
所以,我做了TypeConverter:

@TypeConverter
fun getlistofLongs(data: String?): List<ID> {
    if (data == null) {
        return Collections.emptyList()
    }
    val listType = object : Typetoken<List<Long>>() {}.type

    return gson.fromJson(data, listType)
}

@TypeConverter
fun convertListToString(ids: List<ID>): String = gson.toJson(ids)

我有一个问题,我无法正确查询此列表:

@Query("select count(*) from Message where recipients in (:recipients)")
fun count(recipients: List<ID>?): Int

如您所见,我需要比较两个列表(实体列表和方法列表)并从方法列表中获取包含Id的所有实体,但此查询始终返回0.我读过TypeConverter列表可能有问题,但我可以找不到任何解决方案.

解决方法:

用@RawQuery解决它:

" WHERE recipients LIKE ${recipients.joinToString(",", "\"", "\"")}"

结束编辑TypeConvertor:

@TypeConverter
fun getlistofLongs(data: String?): List<Long> {
    if (data == null) {
        return Collections.emptyList()
    }
    val listType = object : Typetoken<List<Long>>() {}.type

    return gson.fromJson("[$data]", listType)
}

@TypeConverter
fun convertListToString(ids: List<Long>?): String? {
    if (ids == null) return null
    var idsstring = gson.toJson(ids)
    idsstring = idsstring.replace("]", "")
    idsstring = idsstring.replace("[", "")
    return idsstring
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐