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

标签 android sqlite 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,
)

既然是自定义数据类型,我定义了一个类型转换器。

class FollowDatabaseTypeConverter {

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

    @TypeConverter
    fun toString(entityType: FollowEntityType?): String? {
        return entityType?.name
    }
}

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

这是查询。

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

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

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()

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

最佳答案

当我更新 kotlin 库版本而不更新房间数据库版本时,我发生了这个错误。

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

关于android - 查询方法参数应该是可以转换为数据库列的类型或包含此类类型的列表/数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53885749/

相关文章:

java - 在 Android 中跨 http 调用维护 session

android - 为什么标记的信息窗口不显示?

android - 仅使用 mac 地址和密码在 android 应用程序上使用蓝牙

android - 使用 TextView 和 EditText 的自定义 View 不适用于 android :fastForward

spring-data-neo4j v6 : No converter found capable of converting from type [MyDTO] to type [org. neo4j.driver.Value]

Android studio 检查与 API < 19 的兼容性

android - 多个 order by 和 null 值使用下一个字段排序,而不是第一个或最后一个

Objective-C 和 sqlite 的 DATETIME 类型

android - 如何将微调项与数据连接

kotlin - Kotlin将FileTime转换为日,月,年