PersistentCollection is Hibernate’s extension point for collection implementations whose collection state participates in persistence management. It belongs to the collection-mapping problem: connecting Java collection properties with database rows and entity-association lifecycles. section 8.0 section 8.1.3
Use persistent collections to provide framework-managed collection behavior:
- Map a collection property so Hibernate can load its contents on demand while the owning entity remains managed. section 8.1.2
- Add dependent objects through the collection and let cascading persistence make them persistent when the owner is saved. section 8.1.2
- Let the provider manage dependent collection elements during owner deletion, including orphan cleanup in the described composition lifecycle. section 8.1.2
- Implement unsupported collection semantics by extending an existing PersistentSet, PersistentBag, or PersistentList and implementing PersistentCollection. section 8.1.3
A mapped image collection
An Item#images collection can represent all images for one item. Calling someItem.getImages() may trigger SELECT * from IMAGE where ITEM_ID = ?, and adding an Image through someItem.getImages().add() avoids a separate persist call when the Item is saved. The collection represents every image for the item, not an arbitrary filtered query. section 8.1.2
