My problem is that the deletion of all orphan for a collection (set) in a component is not possible if I set the object mapped with component to null. The only solution that I found in order to uptade correctly the database is to set all values of the object (including the collection) to null.
I have the following mapping:
Code:
...
<key column="key1" />
<component name="serialNumber" class="SerialNumber">
<parent name="parent" />
<property name="value1" column="Value1" />
<set name="otherValues" cascade="all-delete-orphan" inverse="true" >
<key column="key1" not-null="true" />
<one-to-many entity-name="OtherValues" />
</set>
</component>
<class entity-name="OtherValues" name="OtherValue" table="OtherValue">
...
<property name="value" column="otherValue" type="string" />
<many-to-one name="parent" class="Serial" column="key1" not-null="true" />
</class>
...
The pojos are the following:
Code:
public class MyClass{
...
private SerialNumber serialNumber;
...
public void setSerialNumber(final SerialNumber serialNumber) {
if (this.serialNumber != null && serialNumber == null) {
// this.serialNumber.setParent(this);
this.serialNumber = null;
}
if (serialNumber != null) {
this.serialNumber = serialNumber;
this.serialNumber.setParent(this);
}
}
}
public class SerialNumber{
private String value1;
private Object parent;
private Set<OtherValue> otherValues;
public void setParent(final Object parent) {
if (parent == null) {
this.value1= null;
this.otherValues.clear();
this.parent = null;
} else {
this.parent = parent;
for (final OtherValue oneOtherValue: this.otherValues) {
oneOtherValue.setParent(parent);
}
}
}
}
If in my code I do MyClass.setSerialNumber(null) with the code "this.serialNumber = null;" in the function "setSerialNumber" and I save, I have the following error:org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: otherValues.
If in my code I do MyClass.setSerialNumber(null) with the code this.serialNumber.setParent(this); in the function "setSerialNumber" and I save, I haven't error and the object is correctly saved in the database but in the session hibernate the object SerialNumber is not null but have all his attributes equal as null.
I would like to find a solution to set the object SerialNumber to null.