I have a
Person class containing an
Address class. Both are mapped as entities as follows:
Code:
<class name="Person" table="Person">
<id name="id" type="int"><generator class="native"/></id>
...
<many-to-one name="address" cascade="all" unique="true" />
</class>
<class name="Address" table="Address">
<id name="id" type="int"><generator class="native"/></id>
...
</class>
Now when I do
Code:
Person person = new Person();
person.setAddress(new Address());
session.saveOrUpdate(person);
The address and person are saved in db. However when I get an existing person and remove the address, the address is removed from the person object but still exists in the db
Code:
Person detachedPerson = service.getDetachedPerson();
detachedPerson.setAddress(null);
session.merge(detachedPerson);
I can imagine that hibernate doesn't delete the Address row from the db for a many-to-one relationship that is not unique, but I would expect that it would for mappings that are explicitly defined as unique.
Is there another way to map a one-to-one association with orphan delete semantics?
I can't specify a cascade="all-delete-orphan" for the many-to-one mapping as this is not allowed in Hibernate.
I can't map Address as a component because we're dealing with a legacy db that is mapped in its own table.
If I would allow a person to have a set of Addresses instead of just one, then I could map it as a one-to-many association that would allow orphan deletes, but that can't be the solution. Can it?
Jan