How to insert null field using Room Android

亡梦爱人 提交于 2021-02-08 05:11:58

问题


I'm trying to play around and understand the Room persistence library of Android. So far I got how to insert things and query them, but now I'm trying to have 2 constructors in order to not insert a field if I dont want to.

Something like this:

    public ImageData(int id, String name, String title, String description, int locationId) {

 .....

      public ImageData(int id, String name, String title, String description) {

In some cases I won't have a location and therefore I don't want to insert anything in that int Location column. Is this the right way to achieve it(apparently not because I get errors)?

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

Please let me know if you need more information.

Edit:

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

回答1:


You should do the following changes, you can use @ignore annotation.

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



回答2:


Method 1

Try using an if statement to determine where location field is empty or has data in it and use the constructors based on the result.


Edit:

So basically, Room database only allows you to use one constructor at a time, so in your case, just delete the one constructor which does not have location in it. This will get rid of the error, but when you are adding a new row to database use this when you do not want to pass in the location id.

database.objectDAO.addObject(id, name, title, description, null);



回答3:


Use the Integer class instead of int.
Same for Boolean instead of boolean and so on




回答4:


Try using default value annotation for the entity fields that can have null value such as: @ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")



来源:https://stackoverflow.com/questions/48088598/how-to-insert-null-field-using-room-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!