Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type(查询方法参数应该是可以转换为数据库列的类型,或者是包含此类类型的列表/数组)
问题描述
我有一个数据库,其自定义数据类型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。将房间更改为2.3.0已修复此错误。
这篇关于查询方法参数应该是可以转换为数据库列的类型,或者是包含此类类型的列表/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!