Why object objects do not work with immutable properties in Android

I was studying the Room library for object database mapping, and I realized something strange.

An entity data model cannot have immutable properties, as this answer suggests .

But I checked the google persistent example with kotlin , Roomalso works with immutable properties. Please check this data class from the example.

What could be causing this behavior?

This can be a good opportunity if we can create immutable values ​​( valproperties), as this restricts programmers from changing unique identifiers, such as identifiers after creating an object.

+11
source share
2 answers

This is strange, because I can make my Entity class using valfor all my fields without problems

@Entity(tableName = "repo")
data class RepoEntity(
        @PrimaryKey @ColumnInfo(name = "id") @SerializedName("id") val id: Int,
        @ColumnInfo(name = "name") @SerializedName("name") val name: String,
        @ColumnInfo(name = "full_name") @SerializedName("full_name") val fullName: String,
        @Embedded(prefix = "owner") @SerializedName("owner") val owner: RepoOwnerEntity,
        @ColumnInfo(name = "html_url") @SerializedName("html_url") val htmlUrl: String,
        @ColumnInfo(name = "description") @SerializedName("description") val description: String?
)

And the data is saved correctly in the database. enter image description here

+2
source

I believe the problem is with certain fields that cannot be constructor parameters. From Javadoc annotations @Relation:

Please note that the annotated field @Relationcannot be a constructor parameter, it must be open or have an open installer.

As a workaround, I had a private constructor parameter _myRelationPropertyand an open field:

val myRelationProperty: List<MyThings> get() = _myRelationProperty
0

All Articles