I have a Room database that contains Stuff entities. These entities have an ID that will be auto-generated:
@Entity(tableName = "stuff")
data class Stuff(val text: String) {
@PrimaryKey(autoGenerate = true) var id: Int = 0
}
There are two things I don't like with my code:
I tried using lateinit var but the compiler wouldn't let me do it on a primitive type. Is there a way to overcome the two issues mentioned above in Kotlin?
RoomDatabase provides direct access to the underlying database implementation but you should prefer using Dao classes. See also: Database.
Is Android Room an ORM? Room isn't an ORM; instead, it is a whole library that allows us to create and manipulate SQLite databases more easily. By using annotations, we can define our databases, tables, and operations.
what do think about solving this using a secondary constructor?
@Entity(tableName = "stuff")
data class Stuff(
@PrimaryKey(autoGenerate = true)
val id: Int,
val text: String
) {
constructor(text: String) : this(0,text)
}
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