android - Ormlite - deleting nested objects from ForeignCollection but not from database -
i have contactitem contains foreigncollection of groupitem:
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; }
and groupitem, contains foreigncollection of contactitem:
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; }
and need delete contactitem groupitem's foreigncollection of contactitems. that:
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","fuck cause problem"); e.printstacktrace(); } }
but delets contactitem whole database. need remove foreigncollection. how can implement this?
i solved issue maybe not elegant, workable solution : set nested object in those, contains null , update object-container 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","fuck cause problem"); e.printstacktrace(); } }
worked me.
Comments
Post a Comment