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;
}
}
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);
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;
}
Try using default value annotation for the entity fields that can have null value such as: @ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With