In my system I have users and one attribute for each:
Code:
<class name="A" table="attribute">
<id name="aId" unsaved-value="-1"><generator class="native"/></id>
<property name="name" type="string" length="20" />
...
</class>
<class name="User" table="users">
<id name="userId" unsaved-value="-1"><generator class="native"/></id>
<many-to-one name="a" class="A" column="aId" cascade="all-delete-orphan" />
...
</class>
Users <-> A relationship could well be one-to-one, but I'm using many-to-one just because it's easier to handle (the idea of one-to-one in Hibernate is quite confusing because in the version I'm using you cannot use foreign keys as reference and you have to have the same primary key value).
When I save new user (or update an existing one) I only need to handle the User object and cascade="all-delete-orphan" handles inserting a new value. But, for some users I'd like to set A to null (and then delete A from attribute table). If I try:
Code:
user.update(); // i need to update some other property values of user
user.getA().delete();
I get error:
Flush during cascade is dangerous - this might occur if an object was deleted and then re-saved by cascade, which is totally understandable I think.
Then, if I try:
Code:
user.setA(null);
the row in attribute table is not deleted although it's set to null in user table. Shouldn't "all-delete-orphans" to the deleting? Or, how can I set A in users table to null and delete the corresponding row from attribute table?