在Room永久性库中使用复合主键时,如何使主键自动递增?

编程入门 行业动态 更新时间:2024-10-27 22:32:22
本文介绍了在Room永久性库中使用复合主键时,如何使主键自动递增?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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永久性库中使用复合主键时,如何使主键自动递增?

本文发布于:2023-10-26 10:12:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529836.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   永久性   库中   Room

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!