【问题标题】:Room @Ignore annotation generates compile time errorRoom @Ignore 注解生成编译时错误
【发布时间】:2018-10-31 05:24:00
【问题描述】:

我正在使用 Room,这是我的数据类

@Entity(tableName = "buy_lead")
class BuyLead(
    @PrimaryKey
    @SerializedName("id")
    val id: String,

    @SerializedName("name")
    val name: String,

    @SerializedName("location")
    val location: String,

    @SerializedName("state")
    val state: String?,

    @SerializedName("message")
    val message: String,

    @SerializedName("product_service")
    val serviceType: String?,

    @SerializedName("posted_on")
    val postedOn: String,

    @SerializedName("formatted_date")
    val formattedDate: String,

    @SerializedName("seconds")
    val seconds: Int,

    @SerializedName("minutes")
    val minutes: Int,

    @SerializedName("hours")
    val hours: Int,

    @SerializedName("days")
    val days: Int,

    var bought: Boolean = false,
    @Ignore var error: String? = null
) {
    val postTime: String
    get() {
        if (seconds < 60) {
            if (seconds == 1)
                return "$seconds second ago"
            return "$seconds seconds ago"
        }

        if (minutes < 60) {
            if (minutes == 1)
                return "$minutes minute ago"
            return "$minutes minutes ago"
        }

        if (hours < 24) {
            if (hours == 1)
                return "$hours hour ago"
            return "$hours hours ago"
        }

        if (days < 5) {
            if (days == 1)
                return "$days day ago"
            return "$days days ago"
        }
        return formattedDate
    }

val address: String
    get() =
        if (state != null) "${location}, ${state}"
        else location
}

它会产生编译时错误

错误:实体和 Pojos 必须有一个可用的公共构造函数。您可以有一个空的构造函数或参数与字段匹配的构造函数(按名称和类型)。 公共最终类 BuyLead { ^

我试图移动类内的字段,但没有任何效果。如果我从错误中删除 @Ignore 注释,它就会编译。我不想在数据库中存储错误字段。

Kotlin 版本是 1.2.71

【问题讨论】:

    标签: android kotlin android-room


    【解决方案1】:

    尝试将主要/默认构造函数添加到您的类。

    像这样,

    class BuyLead()( . . .
    )
    

    另一个例子,

    class ByLead( . . .
        constructor() (
        )
    )
    

    【讨论】:

      【解决方案2】:

      我不知道这是错误还是功能,但 Room 预计会有:

      • 无参数构造函数或
      • 所有字段均未标记@Ignore 的构造函数

      例如:

      @Entity(tableName = "movies")
      data class MovieKt(
          @PrimaryKey
          var id : Int,
          var title: String,
          @Ignore var overview: String) 
      

      不会工作。这将:

      @Entity(tableName = "movies")
      data class MovieKt(
          @PrimaryKey
          var id : Int,
          var title: String) 
      

      【讨论】:

        【解决方案3】:

        一种可能的解决方案是:

        • 使用注释@JvmOverloads 对数据类的构造函数进行解密
        • 将要忽略的字段添加到带有@Ignore注解的构造函数的参数中,并为其分配默认值

        这样,创建了两个构造函数,其中只有一个包含被忽略的字段。只有没有忽略字段的构造函数才会被房间使用。

        这是一个小例子:

        @Entity(tableName = "movie_entity")
        data class MovieEntity @JvmOverloads constructor(
        
          @PrimaryKey
          val id: Int,
        
          val title: String,
        
          @SerializedName("poster_path")
          val posterPath: String,
        
          @SerializedName("genre_ids")
          @Ignore var genreIds: List<Int> = listOf()
        )
        

        【讨论】:

          猜你喜欢
          • 2021-07-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-27
          • 1970-01-01
          • 2018-03-10
          • 1970-01-01
          相关资源
          最近更新 更多