如何使用 Room Android 插入空字段

Bog*_*iel 6 android android-room

我正在尝试尝试并了解 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) {
Run Code Online (Sandbox Code Playgroud)

在某些情况下,我没有位置,因此我不想在 int Location 列中插入任何内容。这是实现它的正确方法吗(显然不是因为我收到错误)?

Error:(38, 12) error: Room cannot pick a constructor since multiple constructors are suitable. Try to annotate unwanted constructors with @Ignore.
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,请告诉我。

编辑:

@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;
        }
}
Run Code Online (Sandbox Code Playgroud)

lib*_*ib4 1

您应该做以下更改,可以使用@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;
    }
Run Code Online (Sandbox Code Playgroud)