我有ContactItem,其中包含GroupItem的ForeignCollection:
public class ContactItem implements Parcelable{
@DatabaseField(generatedId = true)
private Integer id;
@DatabaseField(dataType = DataType.STRING, columnName = Constants.CONTACT_NAME)
private String name;
@DatabaseField(dataType = DataType.STRING, columnName = Constants.CONTACT_NUMBER)
private String number;
@DatabaseField(dataType = DataType.INTEGER, columnName = Constants.CONTACT_ICON)
private int icon;
@DatabaseField(dataType = DataType.INTEGER, columnName = Constants.CONTACT_DAYS)
private int days;
@DatabaseField(foreign=true,canBeNull = true,foreignAutoRefresh = true,columnName = Constants.CONTACT_GROUP)
private GroupItem group;
@ForeignCollectionField(columnName = Constants.CONTACT_GROUP_LIST)
private ForeignCollection<GroupItem> groups;
}
和GroupItem,其中包含ContactItem的ForeignCollection:
public class GroupItem implements Parcelable{
@DatabaseField(generatedId = true)
private Integer id;
@DatabaseField(dataType = DataType.STRING, columnName = Constants.GROUP_NAME)
private String name;
@DatabaseField(dataType = DataType.INTEGER, columnName = Constants.GROUP_ICON)
private int icon;
@DatabaseField(dataType = DataType.INTEGER, columnName = Constants.GROUP_COUNT)
private int count;
@DatabaseField(foreign=true,canBeNull = true,foreignAutoRefresh = true, columnName = Constants.GROUP_CONTACT)
private ContactItem contact;
@ForeignCollectionField(eager = true, columnName=Constants.GROUP_CONTACTS_LIST)
private ForeignCollection<ContactItem> contacts;
@DatabaseField(dataType = DataType.INTEGER, columnName = Constants.GROUP_DAYS)
private int days;
}
我需要从GroupItem的ContactItems的ForeignCollection中删除ContactItem。我这样做:
public void removeContactFromGroup(GroupItem group, ContactItem contact)
{
DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
Dao<GroupItem,Integer> daoGroup = null;
Dao<ContactItem,Integer> daoContact = null;
GroupItem newGroup = null;
try {
daoGroup = db.getGroupDao();
daoContact = db.getContactDao();
UpdateBuilder<GroupItem, Integer> updateBuilder = daoGroup.updateBuilder();
newGroup = daoGroup.queryForId(group.getId());
if ( newGroup.getContactCollection().contains(contact))
{
}
}
catch(Exception e)
{
Log.i(" saveGroupContacts database problem","It is cause problem");
e.printStackTrace();
}
}
但它从整个数据库中删除了ContactItem。但是我只需要从ForeignCollection中删除它。我该如何实现呢?
答案 0 :(得分:0)
我解决了我的问题,可能不是优雅,但可行的解决方案:我只是将我的嵌套对象设置为那些,包含它为null并使用dao更新对象容器:
public void removeContactFromGroup(GroupItem group, ContactItem contact)
{
DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
Dao<GroupItem,Integer> daoGroup = null;
Dao<ContactItem,Integer> daoContact = null;
GroupItem newGroup = null;
ContactItem contactFromDb = null;
try {
daoGroup = db.getGroupDao();
daoContact = db.getContactDao();
UpdateBuilder<GroupItem, Integer> updateBuilder = daoGroup.updateBuilder();
newGroup = daoGroup.queryForId(group.getId());
contactFromDb = daoContact.queryForId(contact.getId());
contactFromDb.setContactGroup(null);
daoContact.update(contactFromDb);
}
catch(Exception e)
{
Log.i(" saveGroupContacts database problem","It is cause problem");
e.printStackTrace();
}
}
为我工作。