查询方法参数应该是可以转换为数据库列的类型或包含此类类型的列表/数组

the*_*ger 4 sqlite android kotlin android-room

我有一个数据库,它有一个自定义数据类型FollowEntityType作为列。

@Entity(primaryKeys = arrayOf("id", "type"), tableName = "follow_info")
data class FollowInfoEntity(
        @ColumnInfo(name = "id") var id: String,
        @ColumnInfo(name = "type") var type: FollowEntityType,
)
Run Code Online (Sandbox Code Playgroud)

由于是自定义数据类型,所以我定义了一个类型转换器。

class FollowDatabaseTypeConverter {

    @TypeConverter
    fun toFollowEntity(entityType: String?): FollowEntityType? {
        return FollowEntityType.from(entityType ?: Constants.EMPTY_STRING)
    }

    @TypeConverter
    fun toString(entityType: FollowEntityType?): String? {
        return entityType?.name
    }
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,我能够在数据库中存储/检索值。但是,在其中一个查询中,构建失败。

这是查询。

@Query("select * from follow_info where type in (:entityTypeList)")
fun getFollowedEntitiesByType(entityTypeList: List<FollowEntityType>) : List<FollowInfoEntity>
Run Code Online (Sandbox Code Playgroud)

构建失败并出现以下错误。

Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
    java.util.List<? extends FollowEntityType> entityTypeList, @org.jetbrains.annotations.NotNull()
Run Code Online (Sandbox Code Playgroud)

错误是针对entityTypeList字段,根据错误,该类型应该是 DB 中的列之一。我已经将FollowEntityType作为列类型之一。我不明白为什么它失败了。请帮助我,因为我没有找到任何解决方案来解决这个问题。

Sam*_* Lu 24

我在Kotlin 1.7.10 + Room 2.4.2中也看到了同样的问题。Kotlin 1.6.21 + Room 2.4.2没有问题。

Kotlin 1.7.10 + Room 2.5.0-alpha02也可以,所以我猜我们必须等待 Room 2.5.0 正式发布才能使用 Kotlin 1.7.x

2022年8月4日:AndroidX团队发布新版本ROOM后。Kotlin 1.7.10 + Room 2.4.3现在可以了。

2022/10/7:如果您使用的是Kotlin 1.9.0或更高版本,则使用Room 2.5.2编译 将不再起作用。您必须使用Kotlin 1.9.0 + Room 2.6.0-rc01(或更高版本)。Google 还建议更改为使用 KSP 而不是 KAPT。我正在使用Kotlin 1.9.0 + KSP 1.9.0-1.0.13 + Room 2.5.2


Vas*_*dev 11

当我在没有更新房间数据库版本的情况下更新 kotlin 库版本时,发生了这个错误。

我使用的是导致问题的 Kotlin 1.5.10 和 Room 2.2.5 版。将 Room 更改为 2.3.0 修复了错误。

  • 将 Kotlin 更新到 1.6.0 后,我遇到了类似的问题,将 Room 从 2.3.0 更新到 2.4.0-beta01 解决了这个问题。 (6认同)

小智 8

当您使用房间查询注释时,从方法中删除挂起。喜欢

老的

@Query("SELECT * FROM userSession where isSync=0 ORDER BY createdOnInMills")
suspend fun getUnSyncSession(): List<SessionModel>
Run Code Online (Sandbox Code Playgroud)

删除暂停

新的

@Query("SELECT * FROM userSession where isSync=0 ORDER BY createdOnInMills")
fun getUnSyncSession(): List<SessionModel>
Run Code Online (Sandbox Code Playgroud)