我有一个数据库,它有一个自定义数据类型 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/