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