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

时间:2018-07-06 10:59:10

标签: kotlin android-room

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

  

错误:无法弄清楚如何将该字段保存到数据库中。您可以考虑为其添加类型转换器。       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


    }

请帮助支持我,谢谢!

1 个答案:

答案 0 :(得分:2)

最后,我认识到Room需要在类型转换器实现中进行一些特殊更改(更改为ArrayList<HashMap<String, OtherServiceEntity>>)。不过,仍然不知道它是功能还是bug,因为在其他情况下,我仍然可以使用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)
    }
}

编码愉快!