gpt4 book ai didi

kotlin - TypeConverters 无法弄清楚如何将此字段保存到数据库中

转载 作者:行者123 更新时间:2023-12-02 13:22:40 25 4
gpt4 key购买 nike

我在实现 Room 的类型转换器时出错。

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it. private java.util.List<? extends java.util.Map<java.lang.String, mypage.profile.entity.OtherServiceEntity>> otherServices;

这是我的实现,首先像下面这样配置实体,因为 POJO 包含 map 列表我这样实现了 Room 的 TypeConverter

object OtherServicesTypeConverter {
private val gson = Gson()
@TypeConverter
@JvmStatic
fun stringToList(data: String?): List<Map<String, OtherServiceEntity>> {
if (data == null) {
return Collections.emptyList()
}

val listType = object : TypeToken<List<Map<String, OtherServiceEntity>>>() {

}.type

return gson.fromJson<List<Map<String, OtherServiceEntity>>>(data, listType)
}

@TypeConverter
@JvmStatic
fun listToString(objects: List<Map<String, OtherService>>): String {
return gson.toJson(objects)
}
}

然后在实体内部使用注释,如下所示使用数据库

@Entity(tableName = "ProfileEntities")
data class ProfileEntity(
@PrimaryKey
@SerializedName("masterId") var masterId: Long = 0,
@SerializedName("nickname") var nickname: String = "",
@TypeConverters(OtherServicesTypeConverter::class)
@SerializedName("otherServices") var otherServices: List<Map<String, OtherServiceEntity>>,
@Embedded
@SerializedName("images") var images: ProfileImagesEntity = ProfileImagesEntity(),
@SerializedName("updatedAt") var updatedAt: String = ""
)


@Database(entities = [ArticleFeatureEntity::class, ProfileEntity::class], version = 3)
@TypeConverters(DataTypeConverter::class, ProfileTypeConverter::class, OtherServicesTypeConverter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun profileDao(): ProfileDao
// Code here


}

请大家帮忙支持我,谢谢!

最佳答案

最后,我认识到 Room 需要对类型转换器实现进行一些特殊更改(更改为 ArrayList<HashMap<String, OtherServiceEntity>>)。不过,不知道这是功能还是错误,因为在其他情况下我仍然可以使用 List 作为类型。这是我所做的细节,现在我可以运行它了

@Entity(tableName = "ProfileEntities")
data class ProfileEntity(
@TypeConverters(ProfileTypeConverter::class)
@SerializedName("details") var details: List<ProfileDetailEntity> = listOf(),
@Embedded
@SerializedName("images") var images: ProfileImagesEntity = ProfileImagesEntity(),
@PrimaryKey
@SerializedName("masterId") var masterId: Long = 0,
@TypeConverters(OtherServicesTypeConverter::class)
@SerializedName("otherServices") var otherServices: ArrayList<HashMap<String, OtherServiceEntity>> = arrayListOf(),
@SerializedName("updatedAt") var updatedAt: String = ""
)

和转换器的实现

class ProfileTypeConverter {
private val gson = Gson()
@TypeConverter
fun stringToList(data: String?): List<ProfileDetailEntity> {
if (data == null) {
return Collections.emptyList()
}

val listType = object : TypeToken<List<ProfileDetailEntity>>() {

}.type

return gson.fromJson<List<ProfileDetailEntity>>(data, listType)
}

@TypeConverter
fun listToString(someObjects: List<ProfileDetailEntity>): String {
return gson.toJson(someObjects)
}
}

class OtherServicesTypeConverter {
private val gson = Gson()
@TypeConverter
fun stringToList(data: String?): ArrayList<HashMap<String, OtherServiceEntity>> {
if (data == null) {
return ArrayList()
}

val listType = object : TypeToken<ArrayList<HashMap<String, OtherServiceEntity>>>() {

}.type

return gson.fromJson<ArrayList<HashMap<String, OtherServiceEntity>>>(data, listType)
}

@TypeConverter
fun listToString(objects: ArrayList<HashMap<String, OtherServiceEntity>>): String {
return gson.toJson(objects)
}
}

编码愉快!

关于kotlin - TypeConverters 无法弄清楚如何将此字段保存到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51208909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com