How to make composite key in Room while using MVVM

2019-04-03 11:58发布

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

2条回答
ら.Afraid
2楼-- · 2019-04-03 12:45

Here is an example

import android.arch.persistence.room.Entity

@Entity(primaryKeys= [ "first_name", "last_name" ] )
class User{
    .......
}
查看更多
狗以群分
3楼-- · 2019-04-03 12:56

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.

查看更多
登录 后发表回答