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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With