我正在与Room持久性库集成.我在Kotlin有一个数据类,如:
@Entity(tableName = "story")
data class Story (
@PrimaryKey val id: Long,
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
)
Run Code Online (Sandbox Code Playgroud)
这些@Entity和@PrimaryKey注释适用于Room库.当我尝试构建时,它失败并出现错误:
Error:Cannot find setter for field.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Run Code Online (Sandbox Code Playgroud)
我也试过提供一个默认的构造函数:
@Entity(tableName = "story")
data class Story (
@PrimaryKey val id: Long,
val by: String,
val descendants: Int,
val score: Int, …Run Code Online (Sandbox Code Playgroud) 我正在使用Room persistent library.我要求在一个表中添加两个主键,其中一个主键应该是自动增量.我不知道实现这一目的的确切语法.下面是我的Model类:
@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;
}
Run Code Online (Sandbox Code Playgroud)
我想让"news_paper_id"自动递增.我该怎么做?
我正在尝试将我的 id 字段设置为从 2018 年开始递增
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private int id = 2018;
Run Code Online (Sandbox Code Playgroud) 我正在使用 RoomDb 使用房间数据库将消息存储在设备中。每个消息都包含一个唯一的 ID,它是在服务器上存储消息时生成的。当消息被下载并存储在 Room 数据库中时,如果我再次尝试下载消息,它会再次下载并保存到 Room db。
我尝试使用替换策略,但仍然不起作用
@Insert(onConflict = OnConflictStrategy.REPLACE)
void saveMessage(ArrayList<Message> messages);
Run Code Online (Sandbox Code Playgroud)
上面的代码应该替换现有的消息,但它没有这样做。
消息模型看起来像这样。
public class Message {
@Exclude
@PrimaryKey(autoGenerate = true)
public long _id;
@Exclude
@ColumnInfo(name = "messageId")
public String id;
@Exclude
public boolean outbox;
@Exclude
public boolean pending;
@Exclude
public boolean draft;
@Exclude
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
public byte[] thumbnail;
@Exclude
public boolean downloaded;
@Exclude
public boolean seen;
@Exclude
public boolean liked;
@Exclude
public boolean disliked;
@Exclude
public String path; // Local attachment path
@Exclude
public …Run Code Online (Sandbox Code Playgroud) 我指的是 Room 数据库的索引特定列。
下面是一些示例代码写在这里https://developer.android.com/training/data-storage/room/defining-data#column-indexing
示例代码-1:
@Entity(indices = {@Index("name"),
@Index(value = {"last_name", "address"})})
public class User {
@PrimaryKey
public int id;
public String firstName;
public String address;
@ColumnInfo(name = "last_name")
public String lastName;
@Ignore
Bitmap picture;
}
Run Code Online (Sandbox Code Playgroud)
示例代码 2:
@Entity(indices = {@Index(value = {"first_name", "last_name"},
unique = true)})
public class User {
@PrimaryKey
public int id;
@ColumnInfo(name = "first_name")
public String firstName;
@ColumnInfo(name = "last_name")
public String lastName;
@Ignore
Bitmap picture;
}
Run Code Online (Sandbox Code Playgroud)
这在 android 的房间文档中有所描述,我使用了索引来表示列的唯一性,但是上面的代码意味着什么,谁能解释一下?
Q1:索引和@Index有什么用?
Q2:是什么区别@Index("name")和@Index(value …