如何使用 Room Android 插入空字段
How to insert null field using Room Android
我正在尝试了解 Android 的 Room 持久性库。到目前为止,我知道了如何插入和查询它们,但现在我正在尝试拥有 2 个构造函数,以便在我不想插入字段时不插入字段。
像这样:
public ImageData(int id, String name, String title, String description, int locationId) {
.....
public ImageData(int id, String name, String title, String description) {
在某些情况下,我没有位置,因此我不想在该 int Location 列中插入任何内容。这是实现它的正确方法吗(显然不是因为我出错了)?
Error:(38, 12) error: Room cannot pick a constructor since multiple constructors are suitable. Try to annotate unwanted constructors with @Ignore.
如果您需要更多信息,请告诉我。
编辑:
@Entity(primaryKeys = {"id", "name"},
foreignKeys = @ForeignKey(entity = Location.class,
parentColumns = "id",
childColumns = "location_id",
onDelete=CASCADE))
public class ImageData {
@NonNull
public int id;
@NonNull
public String name;
public String title;
public String description;
public String time;
@ColumnInfo(name = "location_id")
@Nullable
public int locationId;
public ImageData(int id, String name, String title, String description, int locationId) {
this.id = id;
this.name = name;
this.title = title;
this.description = description;
this.locationId = locationId;
}
public ImageData(int id, String name, String title, String description) {
this.id = id;
this.name = name;
this.title = title;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getName() {
return title;
}
public void setName(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getLocationId() {
return locationId;
}
public void setLocationId(int locationId) {
this.locationId = locationId;
}
}
@Entity
public class Location {
@PrimaryKey(autoGenerate = true)
public int id;
public double latitude;
public double longitude;
public Location(double latitude, double longitude) {
this.latitude= latitude;
this.longitude= longitude;
}
public int getId() {
return this.id;
}
}
方法一
尝试使用 if 语句来确定位置字段的位置为空或其中有数据,并根据结果使用构造函数。
编辑:
所以基本上,Room database
一次只允许您使用一个构造函数,因此在您的情况下,只需删除其中没有位置的一个构造函数。这将消除错误,但是当您向数据库添加新行时,如果您不想传递位置 ID,请使用它。
database.objectDAO.addObject(id, name, title, description, null);
你应该做如下改动,可以使用@ignore注解。
public ImageData(int id, String name, String title, String description, int locationId) {
this.id = id;
this.name = name;
this.title = title;
this.description = description;
this.locationId = locationId;
}
@Ignore
public ImageData(int id, String name, String title, String description) {
this.id = id;
this.name = name;
this.title = title;
this.description = description;
}
使用整数 class 而不是整数。
同样适用于 Boolean 而不是 boolean 等等
尝试对可以具有空值的实体字段使用默认值注释,例如:@ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")
我正在尝试了解 Android 的 Room 持久性库。到目前为止,我知道了如何插入和查询它们,但现在我正在尝试拥有 2 个构造函数,以便在我不想插入字段时不插入字段。
像这样:
public ImageData(int id, String name, String title, String description, int locationId) {
.....
public ImageData(int id, String name, String title, String description) {
在某些情况下,我没有位置,因此我不想在该 int Location 列中插入任何内容。这是实现它的正确方法吗(显然不是因为我出错了)?
Error:(38, 12) error: Room cannot pick a constructor since multiple constructors are suitable. Try to annotate unwanted constructors with @Ignore.
如果您需要更多信息,请告诉我。
编辑:
@Entity(primaryKeys = {"id", "name"},
foreignKeys = @ForeignKey(entity = Location.class,
parentColumns = "id",
childColumns = "location_id",
onDelete=CASCADE))
public class ImageData {
@NonNull
public int id;
@NonNull
public String name;
public String title;
public String description;
public String time;
@ColumnInfo(name = "location_id")
@Nullable
public int locationId;
public ImageData(int id, String name, String title, String description, int locationId) {
this.id = id;
this.name = name;
this.title = title;
this.description = description;
this.locationId = locationId;
}
public ImageData(int id, String name, String title, String description) {
this.id = id;
this.name = name;
this.title = title;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getName() {
return title;
}
public void setName(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getLocationId() {
return locationId;
}
public void setLocationId(int locationId) {
this.locationId = locationId;
}
}
@Entity
public class Location {
@PrimaryKey(autoGenerate = true)
public int id;
public double latitude;
public double longitude;
public Location(double latitude, double longitude) {
this.latitude= latitude;
this.longitude= longitude;
}
public int getId() {
return this.id;
}
}
方法一
尝试使用 if 语句来确定位置字段的位置为空或其中有数据,并根据结果使用构造函数。
编辑:
所以基本上,Room database
一次只允许您使用一个构造函数,因此在您的情况下,只需删除其中没有位置的一个构造函数。这将消除错误,但是当您向数据库添加新行时,如果您不想传递位置 ID,请使用它。
database.objectDAO.addObject(id, name, title, description, null);
你应该做如下改动,可以使用@ignore注解。
public ImageData(int id, String name, String title, String description, int locationId) {
this.id = id;
this.name = name;
this.title = title;
this.description = description;
this.locationId = locationId;
}
@Ignore
public ImageData(int id, String name, String title, String description) {
this.id = id;
this.name = name;
this.title = title;
this.description = description;
}
使用整数 class 而不是整数。
同样适用于 Boolean 而不是 boolean 等等
尝试对可以具有空值的实体字段使用默认值注释,例如:@ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")