如何忽略带有 Room 的实体字段

Ehs*_*san 7 android kotlin data-class android-room

我有一个数据类,我Entity从它为我的数据库创建了一个。

这是我的数据类:

@Entity
@Parcelize
data class Tapligh(

    @PrimaryKey(autoGenerate = true)
    var id: Long,

    @SerializedName("title") var title: String?,
    @SerializedName("type") var type: Int?,
    @SerializedName("os") var os: Int?,
    @SerializedName("logo") var logo: String?,
    @SerializedName("template") var template: String?,
    @SerializedName("action") var action: String?,
    @SerializedName("date") var date: String?,
    @Embedded
    @SerializedName("videos") var videos: Videos?,

) : Parcelable {

    fun getTaplighType(): Int {

        return when (this.type) {

            0 -> TaplighType.IMAGE.type
            1 -> TaplighType.VIDEO.type
            else -> TaplighType.NATIVE.type
        }
    }
}

@Parcelize
data class Videos(

    @SerializedName("land") var land: String?,
    @SerializedName("port") var port: String?
) : Parcelable
Run Code Online (Sandbox Code Playgroud)

现在通过将以下字段添加到我的Tapligh数据类中,我会收到一个错误:

 @Ignore
 @SerializedName("images") var images: List<String>?
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class Tapligh implements android.os.Parcelable {
             ^
Run Code Online (Sandbox Code Playgroud)

gmk*_*k57 9

@Ignore在 Kotlin 类中需要默认参数值和@JvmOverloads注释:

data class Tapligh  @JvmOverloads constructor(
    ...

    @Ignore
    @SerializedName("images") var images: List<String>? = null
)
Run Code Online (Sandbox Code Playgroud)


小智 0

有时,当类实现 Parcelable 时,新字段会产生错误,因此请尝试删除 Parcelable 和类主体,重新放置 Parcelable 并实现成员,或者仅实现 Serialized 接口而不是 Parcelable。