微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么?错误:无法弄清楚如何将该字段保存到数据库中

如何解决为什么?错误:无法弄清楚如何将该字段保存到数据库中

我遇到了愚蠢的问题
我收到服务器的回复

 data class AddressResponse(
    @Serializedname("message") val responseMessage: String,@Serializedname("success") val success: Int,@Serializedname("status") val status: Int,@Serializedname("data") val data: Data
) {
  data class Data(
    @Serializedname("id") val addressId: Long,@Serializedname("status") val status: Int?,@TypeConverters( CommunicationPropertiesConverter::class )
    @Serializedname("communication_properties") val communicationProperties: AddAddressRequest.CommunicationProperties?,@Serializedname("more_information") val moreinformation: String?                                           
  ) {
    data class CommunicationProperties(
      @Serializedname("par1") var par1: Boolean,@Serializedname("par2") val par2: Boolean,@Serializedname("par3") val par3: Boolean,@Serializedname("par4") val par4: Boolean
    )
  }
}

我使用房间并且有实体课

@Entity(tableName = "db_address")
    data class DbAddress(
      @PrimaryKey @ColumnInfo(name = COLUMN_ID) val id: Long,@ColumnInfo(name = COLUMN_STATUS) val status: Int?,@TypeConverters(CommunicationPropertiesConverter::class)
      @ColumnInfo(name = COLUMN_COMM_PROPS) val communicationProperties: AddressResponse.Data.CommunicationProperties,@ColumnInfo(name = COLUMN_MORE_INFO) val moreInfo: String?
    ) 

我有一个转换器,它带有一组注解@TypeConverters和@TypeConverter

    class CommunicationPropertiesConverter {
private val gson = Gson()

     @TypeConverter
      fun toCommunicationProperties(mString: String?): AddressResponse.Data.CommunicationProperties {
        if (mString.isNullOrEmpty())
          return AddressResponse.Data.CommunicationProperties(par1 = false,par2 = false,par3 = false,par4 = false)
        val listType = object : Typetoken<AddressResponse.Data.CommunicationProperties>(){}.type
        return gson.fromJson(mString,listType)
    }

      @TypeConverter
      fun fromCommunicationProperties(properties: AddressResponse.Data.CommunicationProperties?): String {
        if (properties == null)
          return "0000"
        return gson.toJson(properties)
    }
}

但我仍然收到错误消息

错误:无法弄清楚如何将该字段保存到数据库中。您可以 考虑为其添加类型转换器 私有最终... AddressResponse.Data.CommunicationProperties communicationProperties = null

但是我在“原始”类型之间添加了转换器

为什么?我忘记了什么?

解决方法

我遇到了类似的问题,对我有用的是使用 @Embedded 注释该字段(具有自定义类型)。就我而言,我什至不再需要类型转换器了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。