tags:

views:

1011

answers:

2

Hibernate has no support for "delete-orphan" cascading of one-to-one or many-to-one relationships. I recently discovered this, and it's giving me a serious headache. I have a couple classes in my model that were designed such that the child has no real world meaning outside of the parent. I only have one DAO for the parent, and not a separate DAO for the child class.

This works:

parent.getChild().setProperty("something");
parentDao.save(parent);

This doesn't do anything:

parent.setChild(null);
parentDao.save(parent);

This is highly unfortunate because now I have to rethink my DAO layer and a few of the operations of my service layer.

Has anyone worked around this limitation in an elegant way? I'd really like to only concern myself with persisting parent objects. In this particular case, there is no reason to deal with child persistence except to make Hibernate happy, and only in the case of deleting children.

+1  A: 

It appears to be a draw back in the framework they MAY address, hopefully soon

http://opensource.atlassian.com/projects/hibernate/browse/HHH-2608

That ticket was opened in 2007 and the latest post complaining about the problem was as recent as a month ago.

What I am doing is putting a property on my object called isMarkedForDeletion. And if that returns true, i have my dao or my service do an explicit delete. It is definintely NOT as good as delete-orphan would be, but it works well enough in the spring+hibernate framework.

I will keep an eye on this post as this is a VERY important question.

Zoidberg
A: 

The workaround described here https://forum.hibernate.org/viewtopic.php?p=2379216 worked for me. It uses @OneToMany association with a single element on the "many" side to implement a @OneToOne association and solve the remove-orphans problem.

alecswan