I got the following question from a friend the other day.
I have a parent and a child object with a one-to-many relationship by the child’s table having a column that points to it’s parent. What are the correct settings for the mapping in this situation where I need to be able to remove children from the parent’s set, and save the updated parent. It seems like I’ve been through all sorts of combinations, and I was wondering if you could lend me your advice.
The same day I also had a similar question from a colleague at work. I figured I’d post my response here so I could easily find it in the future. I’ve tried to make the code reasonably generic, but watch out for dumb typos.
You should setup the parent mapping like this:
And then map the child like this:
Notice that the column name in each refers to THE SAME COLUMN. You’re just connecting the link.
From the parent class:
From the child class:
This simple example seems to illustrate the basics. My friend later pointed out that one of the things tripping him up was the
cascade option. Recognizing that it’s completely separate from the
inverse property was a big step. That’s an important point – the
inverse just means that the child objects have a property that refers back to the parent. It helps resolve object relationships on load, but the
lazy attributes tell Hibernate when to save and load the objects to and from the database.