liveBook · Manning

Persistent collection

As explained in Java Persistence with Spring Data and Hibernate epub

PersistentCollection is the Hibernate interface for collection implementations that integrate collection state with persistence management.

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

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines the extension point and recommended base implementations, but stops short of teaching custom collection implementation details.
Examples
Uses an Item#images collection, an IMAGE query, and PersistentSet, PersistentBag, and PersistentList as implementation examples.
What it assumes
Assumes basic Java collections and ORM concepts while introducing collection mapping, managed entities, and composition lifecycles.
What you can do after
Apply Hibernate’s existing persistent collection implementations and recognize when custom collection semantics require PersistentCollection.
How it connects
Connects collection mapping with Hibernate, JPA, lazy loading, entity associations, cascading persistence, and orphan removal.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.