如何在 android 房间实体中注释默认值?
How to annotate a default value inside a android room entity?
在查看新 Android 持久性库的 @ColumnInfo 文档时,我找不到任何有关如何注释 SQL - "DEFAULT" 值的信息。
Room 是否为默认值提供注释?
我目前的解决方案是手动创建相应的 Table ...
CREATE TABLE MyTable (
...
MyDefaultValuedCol TEXT DEFAULT 'Default Value',
MyDefaultFlagCol INT DEFAULT 1
)
... 并将 Room 放在顶部。
@Entity(tableName = "MyTable")
class MyClass {
...
public String MyDefaultValuedCol;
public boolean MyDefaultFlagCol;
}
Room 没有任何默认值注释,但您可以在您的实体中设置默认值,如下所示:
@Entity(tableName = "MyTable")
class MyClass {
...
public String MyDefaultValuedCol = "defaultString";
public boolean MyDefaultFlagCol = true;
}
您可以检查实体的 getter 方法并在那里设置一些默认值。
@Entity(tableName = "Dashboard")
public class Dashboard {
@PrimaryKey
@NonNull
@ColumnInfo(name = "claimNumber")
private String claimNumber;
private String percentage = "0";
private String imagePath = "";
@NonNull
public String getClaimNumber() {
return claimNumber;
}
public void setClaimNumber(@NonNull String claimNumber) {
this.claimNumber = claimNumber;
}
public String getPercentage() {
if (percentage == null || percentage.isEmpty()) {
return "0";
}
return percentage;
}
public void setPercentage(String percentage) {
this.percentage = percentage;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public Dashboard(@NonNull String claimNumber, String percentage, String imagePath) {
this.claimNumber = claimNumber;
this.percentage = percentage;
this.imagePath = imagePath;
}
}
对于任何遇到有两个外键并且 "onDelete = CASCADE" 的情况,您可以将外键设置为可以设置为 null 的数据类型
例如:
int parent1Id = 0;
int parent2Id = 0;
//should be:
Long parent1Id = null;
Long parent2Id = null;
通过这种方式,数据库知道某个特定的 Object/row 在删除其父项时试图删除它时没有其他不同类型的父项。
随着房间持久性 2.2.0 的发布,@ColumnInfo 注释中添加了一个新的 属性,可用于指定列的默认值。参见 documentation。
@Entity(tableName = "users")
data class User(
@PrimaryKey val id: Long,
@ColumnInfo(name = "user_name", defaultValue = "temp") val name: String
@ColumnInfo(name = "last_modified", defaultValue = "CURRENT_TIMESTAMP" ) val lastModified: String
)
可以使用@ColumnInfo
注解设置默认值-
@ColumnInfo(defaultValue = "No name")
public String name;
和
@ColumnInfo(defaultValue = "0")
public int flag;
或任何类型的数据
从这里查看参考 Google developer doc
在查看新 Android 持久性库的 @ColumnInfo 文档时,我找不到任何有关如何注释 SQL - "DEFAULT" 值的信息。
Room 是否为默认值提供注释?
我目前的解决方案是手动创建相应的 Table ...
CREATE TABLE MyTable (
...
MyDefaultValuedCol TEXT DEFAULT 'Default Value',
MyDefaultFlagCol INT DEFAULT 1
)
... 并将 Room 放在顶部。
@Entity(tableName = "MyTable")
class MyClass {
...
public String MyDefaultValuedCol;
public boolean MyDefaultFlagCol;
}
Room 没有任何默认值注释,但您可以在您的实体中设置默认值,如下所示:
@Entity(tableName = "MyTable")
class MyClass {
...
public String MyDefaultValuedCol = "defaultString";
public boolean MyDefaultFlagCol = true;
}
您可以检查实体的 getter 方法并在那里设置一些默认值。
@Entity(tableName = "Dashboard")
public class Dashboard {
@PrimaryKey
@NonNull
@ColumnInfo(name = "claimNumber")
private String claimNumber;
private String percentage = "0";
private String imagePath = "";
@NonNull
public String getClaimNumber() {
return claimNumber;
}
public void setClaimNumber(@NonNull String claimNumber) {
this.claimNumber = claimNumber;
}
public String getPercentage() {
if (percentage == null || percentage.isEmpty()) {
return "0";
}
return percentage;
}
public void setPercentage(String percentage) {
this.percentage = percentage;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public Dashboard(@NonNull String claimNumber, String percentage, String imagePath) {
this.claimNumber = claimNumber;
this.percentage = percentage;
this.imagePath = imagePath;
}
}
对于任何遇到有两个外键并且 "onDelete = CASCADE" 的情况,您可以将外键设置为可以设置为 null 的数据类型 例如:
int parent1Id = 0;
int parent2Id = 0;
//should be:
Long parent1Id = null;
Long parent2Id = null;
通过这种方式,数据库知道某个特定的 Object/row 在删除其父项时试图删除它时没有其他不同类型的父项。
随着房间持久性 2.2.0 的发布,@ColumnInfo 注释中添加了一个新的 属性,可用于指定列的默认值。参见 documentation。
@Entity(tableName = "users")
data class User(
@PrimaryKey val id: Long,
@ColumnInfo(name = "user_name", defaultValue = "temp") val name: String
@ColumnInfo(name = "last_modified", defaultValue = "CURRENT_TIMESTAMP" ) val lastModified: String
)
可以使用@ColumnInfo
注解设置默认值-
@ColumnInfo(defaultValue = "No name")
public String name;
和
@ColumnInfo(defaultValue = "0")
public int flag;
或任何类型的数据 从这里查看参考 Google developer doc