我正在使用Room持久性库.我需要在一个表中添加两个主键,并且其中一个主键应该是自动递增.我不知道确切的语法来实现这一目标.下面是我的Model类:
I am using Room persistent library. I have requirement to add two primary keys in one table and one of the primary key should be auto increment. I don't know exact syntax to achieve this. Below is my Model class:
@Entity(tableName = "newsPapers", primaryKeys = {"news_paper_id","news_paper_name"}) public class SelectNewsModel { private int news_paper_id; @ColumnInfo(name = "image_url") private String imageUrl; @ColumnInfo(name = "news_paper_name") private String newsPaperName; }我想使"news_paper_id"自动递增.我该怎么做?
I want to make "news_paper_id" to be auto incremented. How can i make it?
推荐答案我找到了解决此问题的另一种方法,因为据我的某些研究和开发经验,我们在复合主键中不能具有自动递增属性.所以我在这里使用了索引和唯一约束,因为到目前为止,Room还没有直接的UNIQUE约束.所以下面是我的工作代码:
I found another way around for this problem because as per my knowledge after some R&D, we can not have auto increment property in Composite Primary keys. So I used indices and unique constraint here because Room does not have direct UNIQUE constraint till now. So below is my working code:
@Entity(tableName = "newsPapers", indices = {@Index(value = {"news_paper_name"}, unique = true)}) public class SelectNewsModel { @PrimaryKey(autoGenerate = true) private int news_paper_id; @ColumnInfo(name = "image_url") private String imageUrl; @ColumnInfo(name = "news_paper_name") private String newsPaperName; }更多推荐
在Room永久性库中使用复合主键时,如何使主键自动递增?
发布评论