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
