17

I have been exploring Room database object mapping library and I figured something weird.

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

But I checked out google's persistent example with kotlin, Room works with immutable properties too. Please check this data class from the example.

What could be the reason for this behavior?

This could be a good feature if we could create immutable values (val properties), as this restrict programmers from changing unique identifiers such as ids after an object has been created.

Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
  • I'm not an expert on Room specifically, but I believe if you want to use `val`, you need to give default values for everything since it might be relying on a default constructor or something unless this has changed recently. Also, you won't be able to change values once they are set. – Jan Vladimir Mostert Nov 16 '17 at 09:50
  • @JanVladimirMostert Thanks for the reply. I did test that with default values, still the problem seem to occur – Samuel Robert Nov 16 '17 at 10:14

2 Answers2

4

It's weird because I can make my Entity class using val for all of my fields without an issue

@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 still stored correctly inside the Database. enter image description here

Khairil Ushan
  • 2,358
  • 5
  • 26
  • 29
1

I believe that the issue stems from certain fields which can not be constructor parameters. From the Javadoc of the @Relation annotation:

Note that the @Relation annotated field cannot be a constructor parameter, it must be public or have a public setter.

As a workaround, I had a private constructor parameter _myRelationProperty and a public field:

val myRelationProperty: List<MyThings> get() = _myRelationProperty
Zoe
  • 27,060
  • 21
  • 118
  • 148
Mitchell Skaggs
  • 310
  • 3
  • 11