外部コレクションが埋め込まれたオブジェクトを永続化する正しい方法は何ですか? 現在、以下を使用していますがCould not create data element in dao
、zonePlay が既に存在する場合に、「zoneplay」オブジェクトを顧客の zonePlays コレクションに追加すると、エラーが発生します。外部コレクションに追加するために使用する必要がある upsert メソッドがありますか、または挿入の前に何らかの形で存在を確認する必要がありますか?
@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);