2

我有联系和小组课程。每个联系人可以在多个组中,一个组可以有很多联系人。我的课程:

@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;
}

当我需要将联系人列表添加到指定组时,我会执行以下操作:

 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();
                 }
}

但得到

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

我怎样才能解决这个问题?

4

1 回答 1

0

在这种情况下,我解决了我的问题:

 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();
                 }

            }
于 2013-08-28T13:00:07.940 回答