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

How to make Primary key Auto increment while using Composite Primary keys in Room persistent library?

我正在使用 Room 持久库。我要求在一个 table 中添加两个主键,其中一个主键应该是自动递增的。我不知道实现此目的的确切语法。下面是我的模型 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" 自动递增。我怎样才能做到?

我找到了解决这个问题的另一种方法,因为据我所知,经过一些研发,我们不能在复合主键中自动递增 属性。所以我在这里使用了索引和唯一约束,因为 Room 直到现在还没有直接的 UNIQUE 约束。所以下面是我的工作代码:

@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;
}

Priyanka Alachiya 的回答是正确的,但我需要 Kotlin 示例...

对于 Kotlin:

@Entity(tableName = "newsPapers", indices = arrayOf(Index(value = ["news_paper_name"], unique = true)))