Question

I have contact and group classes. Each contact can be in many groups and one group can have a lot of contacts. My classes:

@DatabaseTable(tableName = "contacts")
public class ContactItem implements Parcelable{
    private static final long serialVersionUID = 1L;
    @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)
}

@DatabaseTable(tableName = "groups")
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;
}

And when I need to add list of contacts to the specified group I do something like that :

 public void saveGroupContacts(GroupItem group, ArrayList<ContactItem> contacts)
        {
                DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
                Dao<GroupItem,Integer> daoGroup = null;
                GroupItem newGroup;
                 try {
                     daoGroup = db.getGroupDao();
                     UpdateBuilder<GroupItem, Integer> updateBuilder = daoGroup.updateBuilder();
                     newGroup = daoGroup.queryForId(group.getId());
                     Log.i(" received group from database", newGroup.toString());
                     ForeignCollection<ContactItem> fAp =
                             daoGroup.getEmptyForeignCollection(Constants.GROUP_CONTACTS_LIST);
                     Log.i("foreign coll size", Integer.toString(fAp.size()));
                     for (ContactItem contact: contacts)
                     {
                         Log.i("contact to save in group", contact.toString());
                         contact.setContactGroup(group);
                         group.setContact(contact);
             fAp.add(contact);
                     }
                     daoGroup.update(newGroup);
                 }
                 catch(Exception e)
                 {
                     e.printStackTrace();
                 }
}

but get the

 java.lang.IllegalStateException: Could not create data element in dao

How can I fix this?

Was it helpful?

Solution

I solved my issue in this case:

 public void saveNonExistingGroupContacts(GroupItem group, ArrayList<ContactItem> contacts)
        {
                DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
                Dao<GroupItem,Integer> daoGroup = null;
                Dao<ContactItem,Integer> daoContact = null;
                GroupItem newGroup;
                 try {
                     daoGroup = db.getGroupDao();
                     daoContact= db.getContactDao();
                     UpdateBuilder<ContactItem, Integer> contactUpdateBuilder = daoContact.updateBuilder();
                     UpdateBuilder<GroupItem, Integer> groupUpdateBuilder = daoGroup.updateBuilder();
                     daoGroup.createIfNotExists(group);
                     newGroup = daoGroup.queryForId(group.getId());
                     ForeignCollection<ContactItem> fAp =
                             daoGroup.getEmptyForeignCollection(Constants.GROUP_CONTACTS_LIST);
                     for (ContactItem contact: contacts)
                     {
                         contact.setContactGroup(group);
                         newGroup.setContact(contact);
                         newGroup.setCount(newGroup.getCount()+1);
                         contactUpdateBuilder.updateColumnValue(Constants.CONTACT_GROUP,newGroup);
                         groupUpdateBuilder.updateColumnValue(Constants.GROUP_CONTACT,contact);
                     }
                     contactUpdateBuilder.update();
                     groupUpdateBuilder.update();
                 }
                 catch(Exception e)
                 {

                     e.printStackTrace();
                 }

            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top