I have a simple "1:M" mapping between a group and a set of users. I need to move all users from one group to another group. I'm having a difficult time doing this when the cascade for the set of user set to "all-delete-orphan". If I set cascade to "all" this works fine. Is there any way to make this work when cascade is set to "all-delete-orphan"?
BTW: I'm a newbie to Hibernate and am very impressed. Nice job Hibernate team!
Hibernate version: 2.1
Mapping documents:
Code:
<class name="Group" table="my_group">
<id name="id" type="long" unsaved-value="null" >
<column name="id" sql-type="long" not-null="true"/>
<generator class="hilo">
... HI LOW PARAMS ...
</generator>
</id>
<!-- PROPERTIES LEFT OUT ... -->
<set name="users" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="group_id"/>
<one-to-many class="User"/>
</set>
</class>
<class name="User" table="my_user">
<id name="id" type="long" unsaved-value="null" >
<column name="id" sql-type="long" not-null="true"/>
<generator class="hilo">
... HI LOW PARAMS ...
</generator>
</id>
<!-- PROPERTIES LEFT OUT ... -->
<many-to-one name="group" class="Group" column="group_id" not-null="true"/>
</class>
Code between sessionFactory.openSession() and session.close():
Code:
Group fromGroup = ...load from group ...
Group toGroup = ...load to group ...
transaction = session.newTransaction();
Set users = new HashSet(fromGroup.getUsers());
Iterator it = users.iterator();
while (it.hasNext()) {
User user = (User)it.next();
fromGroup.getUsers().remove(user);
user.setGroup(toGroup);
toGroup.getUsers().add(user);
}
session.save(fromGroup);
session.save(toGroup);
transaction.commit();
Full stack trace of any exception that occurs:
Code:
CAUSE: deleted object would be re-saved by cascade (remove deleted object from associations): 1002002, of class: User
Name and version of the database you are using: SQL ServerThe generated SQL (show_sql=true):Debug level Hibernate log excerpt:Code: