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 issueAnd the data still stored correctly inside the Database.
I believe that the issue stems from certain fields which can not be constructor parameters. From the Javadoc of the
@Relation
annotation:As a workaround, I had a private constructor parameter
_myRelationProperty
and a public field:val myRelationProperty: List<MyThings> get() = _myRelationProperty