Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert null field using Room Android

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;
        }
}
like image 971
Bogdan Daniel Avatar asked Jan 04 '18 04:01

Bogdan Daniel


3 Answers

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);
like image 168
hysabone.com Avatar answered Oct 08 '22 06:10

hysabone.com


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;
    }
like image 1
lib4 Avatar answered Oct 08 '22 05:10

lib4


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

like image 1
Xokarca Avatar answered Oct 08 '22 05:10

Xokarca