What's the correct way to persist an object with an embedded foreign collection? Currently I am using the following but I get an error Could not create data element in dao
if I am adding a "zoneplay" object to the customer zonePlays collection when the zonePlay already exists. Is there some sort of upsert method I should use to add to the foreign collection or do I need to check for existance somehow before the insert?
@DatabaseTable
public class Customer {
@DatabaseField(id = true)
public int id;
@ForeignCollectionField(eager = true)
public ForeignCollection<ZonePlay> zonePlays;
@DatabaseField
public String email;
public Customer(String json) {
final JSONArray zonesJson = customerJson.getJSONArray("zoneplays");
this.zonePlays = DatabaseHelper.getInstance(ctx).getCustomerDao().getEmptyForeignCollection("zonePlays");
for (int i = 0; i < numZones; i++) {
final JSONObject rawZone = zonesJson.getJSONObject(i);
ZonePlay zp = new ZonePlay();
zp.id = rawZone.getInt("id");
zp.score = rawZone.getInt("score");
zp.Customer = this;
this.zonePlays.add(zp);
}
}
@DatabaseTable
public class ZonePlay {
@DatabaseField(id = true)
public int id;
@DatabaseField(foreign=true)
public Customer customer;
}
Then I run this
Customer cust = new Customer(client.response);
DatabaseHelper db = DatabaseHelper.getInstance(getApplicationContext());
db.getDao(Customer.class).createOrUpdate(cust);
What's the correct way to persist an object with an embedded foreign collection?
The "correct" way may be to add the element using it's own DAO -- not through the foreign collection. This way you can use the createOrUpdate(...)
method:
Dao<ZonePlay, Integer> zoneDao = DatabaseHelper.getInstance(ctx).getZoneDao();
for (int i = 0; i < numZones; i++) {
final JSONObject rawZone = zonesJson.getJSONObject(i);
ZonePlay zp = new ZonePlay();
zp.id = rawZone.getInt("id");
zp.score = rawZone.getInt("score");
zp.Customer = this;
zoneDao.createOrUpdate(zp);
}
I get an error Could not create data element in dao if I am adding a "zoneplay" object to the customer zonePlays collection when the zonePlay already exists.
Right. If you are trying to add a ZonePlay
to a Customer
's foreign collection, this will try to add to the table. If a ZonePlay
already exists in the table then this will throw an exception.