Unable to find setter for field - using Kotlin database with room

I integrate with the Persistence library. I have a data class in Kotlin:

@Entity(tableName = "story")
data class Story (
        @PrimaryKey val id: Long,
        val by: String,
        val descendants: Int,
        val score: Int,
        val time: Long,
        val title: String,
        val type: String,
        val url: String
)

Annotations @Entityand @PrimaryKeyare intended for the Room library. When I try to build, it fails with an error:

Error:Cannot find setter for field.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

I also tried to provide a default constructor:

@Entity(tableName = "story")
data class Story (
        @PrimaryKey val id: Long,
        val by: String,
        val descendants: Int,
        val score: Int,
        val time: Long,
        val title: String,
        val type: String,
        val url: String
) {
    constructor() : this(0, "", 0, 0, 0, "", "", "")
}

But that does not work. It should be noted that it works if I convert this Kotlin class to a Java class using getters and setters. Any help is appreciated!

+51
source share
10 answers

Since your fields are checked val, they are actually final and have no settings fields.

val var. .

@Entity(tableName = "story")
data class Story (
        @PrimaryKey var id: Long? = null,
        var by: String = "",
        var descendants: Int = 0,
        var score: Int = 0,
        var time: Long = 0L,
        var title: String = "",
        var type: String = "",
        var url: String = ""
)
+132

, , , , is Room. ,

   @Entity(tableName = "user")
   data class User (
        @PrimaryKey var id: Long? = null,
        var userName: String = "",
        var isConnectedToFB: Boolean = false,
)
+24

Java- db.

isFavorite. , favorite .

var isFavorite: Int? = 0, var isFavorite: Int? = 0, var favorite: Int? = 0, var favorite: Int? = 0,

+4

fooobar.com/questions/252524/... , :

@Entity(tableName = "story")
data class Story (
        val by: String,
        val descendants: Int,
        val score: Int,
        val time: Long,
        val title: String,
        val type: String,
        val url: String
)  {
    @PrimaryKey(autoGenerate = true)
    var id: Int = 0
}

, @PrimaryKey var.

, :

val newStory = story.copy(by = "new author", title = "new title") // Cannot use "id" in object cloning
newStory.id = story.id
dao.update(newStory)

AndroidX, Room - "android.arch.persistence.room:runtime:1.1.1".

Serializable. Parcelable, ( id): Property would not be serialized inro a 'Parcel'. Add '@IgnoredOnParcel' annotation to remove this warning Property would not be serialized inro a 'Parcel'. Add '@IgnoredOnParcel' annotation to remove this warning:

enter image description here

id . Kotlin @Parcelize Parcelable:

@Parcelize
@Entity(tableName = "story")
data class Story (
    @PrimaryKey(autoGenerate = true)
    var id: Int = 0,

    val by: String,
    val descendants: Int,
    val score: Int,
    val time: Long,
    val title: String,
    val type: String,
    val url: String
) : Parcelable
+3

Java.

, is is_ Java.

.

:

, .

:

public MyEntity(String name, ...) {
   this.name = name;
   ...
}

public void setName(String name) {
    this.name = name;
}
+2
+1

, - 2019 , , , , .

val , androidx.room:room-<any>:2.* (androidx.room:room-<any>:2.*), android.arch.persistence.room:<any>:1.1.1 , 2.* .

:

+1

, val, .

  1. AndroidX.
  2. ,
  3. , 2.0.0-01
  4. val default value :
@Entity("tbl_abc")
data class Abc(
    @PrimaryKey
    val id: Int = 0, 
    val isFavourite: Boolean = false
)

Cannot find setter for field. var - , , .

+1

Now you can start your field with is, but you cannot have a number next to is, for example: is2FooSelectedyou must rename to isTwoFooSelected.

0
source

All Articles