Composite Elements
From Zanecorpwiki
Composite elements are Hibernate's version of the struct. They're data collections that don't have an independent identity, but exist only in the context of the owning/parent entity. Composite elements may not be referenced from other elements (they're not shared) and they cannot themselves reference collections. You can, however, have a collection of composites. If these two limitations are acceptable, then use composites.
The advantage is that composites are easier and safer in terms of handling updates and saves. With independent objects, you have to deal with making sure that referenced objects are saved, and saved in the proper order. Whether manual or through configuration settings. However, a composite element is, despite being in many parts and possibly being a collection, is treated like a private, primitive field when it comes to saves and updates.
When using composite elements in a collection (list, bag, etc.), it's vital to override equals() and hashCode() and get them right. If you don't, you could end up with very unhelpful errors that give no clue as to the underlying problem. What does it mean to "get them right"? Base them off the business object keys.


