0
使用嵌入式外部集合保持對象的正確方法是什麼?目前,我正在使用以下內容,但如果我在zonePlay已存在時將「zoneplay」對象添加到customer zonePlays集合,則會收到錯誤Could not create data element in dao
。是否有某種upsert方法可用於添加到外部集合中,還是需要在插入之前檢查是否存在某種方式?理解/使用ORMLite中的ForeignCollectionField時遇到困難
@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;
}
然後我跑這個
Customer cust = new Customer(client.response);
DatabaseHelper db = DatabaseHelper.getInstance(getApplicationContext());
db.getDao(Customer.class).createOrUpdate(cust);