liveBook · Manning

Collection mapping

As explained in Java Persistence with Spring Data and Hibernate epub

The specification of how an object collection property corresponds to relational database structures and persistence behavior.

Collection mapping connects a Java collection property to relational data and defines how its elements are loaded and persisted. It can provide navigational access, but it is optional: a query may be simpler when the application rarely needs the whole collection. section 8.1.2 section 9.0

Start with the schema and the relationship it represents. An Item can expose its images through an images property rather than requiring a separate query each time. For managed instances, accessing the collection can load the related IMAGE rows on demand. Iteration retrieves the collection for that item, however; it does not substitute for a query with additional filtering criteria. section 8.1 section 8.1.2

Choose the mapping to fit both the domain and the required behavior:

  • Use an appropriate Java collection interface: sets, identifier bags, lists, maps, and sorted or ordered collections have distinct mapping options. Collections can hold basic values or embeddable types as well as entity references. section 8.1 section 8.4
  • Decide whether a child is a value or an entity. A Bid that has no independent references may be embeddable; a Bid referenced by another table needs to be a shared entity. section 8.3.2
  • Choose lifecycle behavior deliberately. A mapped collection can support cascading persistence or other selected state changes; dependent images can also be deleted when their Item is deleted. section 8.1.2 section 8.3.2

A bidirectional entity collection

An Item can declare `@OneToMany(mappedBy = "item", fetch = FetchType.LAZY) private Set<Bid> bids = new HashSet<>();`. The `mappedBy` value names the property on Bid. Iterating an Item’s bids can trigger loading of its related BID rows; configured cascading can propagate selected state changes from the Item. section 8.3.2

Do not add a collection solely because a foreign key exists. A query or a many-to-one association may meet the need with less Java code and mapping complexity. More elaborate one-to-many, many-to-many, and ternary mappings warrant the same test: will collection navigation or its lifecycle behavior actually be used? section 8.1.2 section 8.3.2 section 9.0

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Works through collection choices and a bidirectional one-to-many mapping, while deferring more advanced entity associations.
Examples
Uses Item image and bid collections, an IMAGE query, and a bidirectional `Set<Bid>` mapping.
What it assumes
Assumes familiarity with Java collections and relational tables; introduces collection properties before more complex association mappings.
What you can do after
Apply it via JPA or Hibernate to choose whether to map a collection and configure its navigation and lifecycle behavior.
How it connects
Links Java Collections, basic types, embeddables, entity associations, lazy loading, and cascading.

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.