Chapter 21. Example: Parent/Child

Chapter 21. Example: Parent/Child

21.1. A note about collections
21.2. Bidirectional one-to-many
21.3. Cascading lifecycle
21.4. Cascades and unsaved-value
21.5. Conclusion

One of the very first things that new users try to do with Hibernate is to model a parent / child type relationship. There are two different approaches to this. For various reasons the most convenient approach, especially for new users, is to model both Parent and Child as entity classes with a <one-to-many> association from Parent to Child. (The alternative approach is to declare the Child as a <composite-element>.) Now, it turns out that default semantics of a one to many association (in Hibernate) are much less close to the usual semantics of a parent / child relationship than those of a composite element mapping. We will explain how to use a bidirectional one to many association with cascades to model a parent / child relationship efficiently and elegantly. It's not at all difficult!