How to make composite key in Room while using MVVM

2019-04-03 12:35发布

问题:

I have just found @PrimaryKey annotation in room. So If I want to make composite key so how can I make that in MVVM ?

回答1:

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:

@Entity(primaryKeys = {"column1","column2","column3"})
class DummyClass {
    ...
}

Thanks Lalit Kushwah for the example.



回答2:

Here is an example

import android.arch.persistence.room.Entity

@Entity(primaryKeys= [ "first_name", "last_name" ] )
class User{
    .......
}