How to make composite key in Room
Here is an example for Kotlin:
import android.arch.persistence.room.Entity
@Entity(primaryKeys= [ "first_name", "last_name" ] )
class User{
.......
}
This worked for me I'm using Kotlin 1.3, I think.
@Entity(tableName = "location_table", primaryKeys = ["lat", "lon"])
data class MyLocation(
// @PrimaryKey(autoGenerate = true) var id: Long?,
var lat: Double,
var lon: Double,
var dateTime: String,
var weatherDescription: String,
var temperature: Double
)
Make use of primaryKeys()
.
Android Developer Documentation for Room
states:
If PrimaryKey annotation is used on a Embeddedd field, all columns inherited from that embedded field becomes the composite primary key (including its grand children fields).
Example implementation in Java:
@Entity(primaryKeys = {"column1","column2","column3"})
public class DummyClass {
...
}
Example implementation in kotlin:
@Entity(primaryKeys = ["column1","column2","column3"])
class DummyClass {
...
}
Thanks Lalit Kushwah for the example.