无法在ForeignCollection ORMLite中创建数据元素



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

@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

我该怎么解决这个问题?

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

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

相关内容

  • 没有找到相关文章

最新更新