Skip to content

Latest commit

 

History

History
875 lines (678 loc) · 31.6 KB

File metadata and controls

875 lines (678 loc) · 31.6 KB

Collections

Naturally Hibernate also allows persisting collections. These persistent collections can contain almost any other Hibernate type, including basic types, custom types, embeddables, and references to other entities. In this context, the distinction between value and reference semantics is very important. An object in a collection might be handled with value semantics (its lifecycle being fully dependant on the collection owner), or it might be a reference to another entity with its own lifecycle. In the latter case, only the link between the two objects is considered to be a state held by the collection.

The owner of the collection is always an entity, even if the collection is defined by an embeddable type. Collections form one/many-to-many associations between types so there can be:

  • value type collections

  • embeddable type collections

  • entity collections

Hibernate uses its own collection implementations which are enriched with lazy-loading, caching or state change detection semantics. For this reason, persistent collections must be declared as an interface type. The actual interface might be java.util.Collection, java.util.List, java.util.Set, java.util.Map, java.util.SortedSet, java.util.SortedMap or even other object types (meaning you will have to write an implementation of org.hibernate.usertype.UserCollectionType).

As the following example demonstrates, it’s important to use the interface type and not the collection implementation, as declared in the entity mapping.

Example 1. Hibernate uses its own collection implementations
link:../../../../../test/java/org/hibernate/userguide/collections/BasicTypeElementCollectionTest.java[role=include]

link:../../../../../test/java/org/hibernate/userguide/collections/BasicTypeElementCollectionTest.java[role=include]
Note

It is important that collections be defined using the appropriate Java Collections Framework interface rather than a specific implementation.

From a theoretical perspective, this just follows good design principles. From a practical perspective, Hibernate (like other persistence providers) will use their own collection implementations which conform to the Java Collections Framework interfaces.

The persistent collections injected by Hibernate behave like ArrayList, HashSet, TreeSet, HashMap or TreeMap, depending on the interface type.

Collections as a value type

Value and embeddable type collections have a similar behavior to basic types since they are automatically persisted when referenced by a persistent object and automatically deleted when unreferenced. If a collection is passed from one persistent object to another, its elements might be moved from one table to another.

Important

Two entities cannot share a reference to the same collection instance. Collection-valued properties do not support null value semantics because Hibernate does not distinguish between a null collection reference and an empty collection.

Collections of value types

Collections of value type include basic and embeddable types. Collections cannot be nested, and, when used in collections, embeddable types are not allowed to define other collections.

For collections of value types, JPA 2.0 defines the @ElementCollection annotation. The lifecycle of the value-type collection is entirely controlled by its owning entity.

Considering the previous example mapping, when clearing the phone collection, Hibernate deletes all the associated phones. When adding a new element to the value type collection, Hibernate issues a new insert statement.

Example 2. Value type collection lifecycle
link:../../../../../test/java/org/hibernate/userguide/collections/BasicTypeElementCollectionTest.java[role=include]
link:extras/collections/collections-value-type-collection-lifecycle-example.sql[role=include]

If removing all elements or adding new ones is rather straightforward, removing a certain entry actually requires reconstructing the whole collection from scratch.

Example 3. Removing collection elements
link:../../../../../test/java/org/hibernate/userguide/collections/BasicTypeElementCollectionTest.java[role=include]
link:extras/collections/collections-value-type-collection-remove-example.sql[role=include]

Depending on the number of elements, this behavior might not be efficient, if many elements need to be deleted and reinserted back into the database table. A workaround is to use an @OrderColumn, which, although not as efficient as when using the actual link table primary key, might improve the efficiency of the remove operations.

Example 4. Removing collection elements using @OrderColumn
link:../../../../../test/java/org/hibernate/userguide/collections/BasicTypeOrderColumnElementCollectionTest.java[role=include]

link:../../../../../test/java/org/hibernate/userguide/collections/BasicTypeOrderColumnElementCollectionTest.java[role=include]
link:extras/collections/collections-value-type-collection-order-column-remove-example.sql[role=include]
Note

The @OrderColumn column works best when removing from the tail of the collection, as it only requires a single delete statement. Removing from the head or the middle of the collection requires deleting the extra elements and updating the remaining ones to preserve element order.

Embeddable type collections behave the same way as value type collections. Adding embeddables to the collection triggers the associated insert statements and removing elements from the collection will generate delete statements.

Example 5. Embeddable type collections
link:../../../../../test/java/org/hibernate/userguide/collections/EmbeddableTypeElementCollectionTest.java[role=include]

link:../../../../../test/java/org/hibernate/userguide/collections/EmbeddableTypeElementCollectionTest.java[role=include]
link:extras/collections/collections-embeddable-type-collection-lifecycle-example.sql[role=include]

Collections of entities

If value type collections can only form a one-to-many association between an owner entity and multiple basic or embeddable types, entity collections can represent both @OneToMany and @ManyToMany associations.

From a relational database perspective, associations are defined by the foreign key side (the child-side). With value type collections, only the entity can control the association (the parent-side), but for a collection of entities, both sides of the association are managed by the persistence context.

For this reason, entity collections can be devised into two main categories: unidirectional and bidirectional associations. Unidirectional associations are very similar to value type collections since only the parent side controls this relationship. Bidirectional associations are more tricky since, even if sides need to be in-sync at all times, only one side is responsible for managing the association. A bidirectional association has an owning side and an inverse (mappedBy) side.

Another way of categorizing entity collections is by the underlying collection type, and so we can have:

  • bags

  • indexed lists

  • sets

  • sorted sets

  • maps

  • sorted maps

  • arrays

In the following sections, we will go through all these collection types and discuss both unidirectional and bidirectional associations.

Bags

Bags are unordered lists, and we can have unidirectional bags or bidirectional ones.

Unidirectional bags

The unidirectional bag is mapped using a single @OneToMany annotation on the parent side of the association. Behind the scenes, Hibernate requires an association table to manage the parent-child relationship, as we can see in the following example:

Example 6. Unidirectional bag
link:../../../../../test/java/org/hibernate/userguide/collections/UnidirectionalBagTest.java[role=include]
link:extras/collections/collections-unidirectional-bag-example.sql[role=include]
Note

Because both the parent and the child sides are entities, the persistence context manages each entity separately.

The cascading mechanism allows you to propagate an entity state transition from a parent entity to its children.

By marking the parent side with the CascadeType.ALL attribute, the unidirectional association lifecycle becomes very similar to that of a value type collection.

Example 7. Unidirectional bag lifecycle
link:../../../../../test/java/org/hibernate/userguide/collections/UnidirectionalBagTest.java[role=include]
link:extras/collections/collections-unidirectional-bag-lifecycle-example.sql[role=include]

In the example above, once the parent entity is persisted, the child entities are going to be persisted as well.

Note

Just like value type collections, unidirectional bags are not as efficient when it comes to modifying the collection structure (removing or reshuffling elements).

Because the parent-side cannot uniquely identify each individual child, Hibernate deletes all link table rows associated with the parent entity and re-adds the remaining ones that are found in the current collection state.

Bidirectional bags

The bidirectional bag is the most common type of entity collection. The @ManyToOne side is the owning side of the bidirectional bag association, while the @OneToMany is the inverse side, being marked with the mappedBy attribute.

Example 8. Bidirectional bag
link:../../../../../test/java/org/hibernate/userguide/collections/BidirectionalBagTest.java[role=include]
link:extras/collections/collections-bidirectional-bag-example.sql[role=include]
Example 9. Bidirectional bag lifecycle
link:../../../../../test/java/org/hibernate/userguide/collections/BidirectionalBagTest.java[role=include]
link:extras/collections/collections-bidirectional-bag-lifecycle-example.sql[role=include]
Example 10. Bidirectional bag with orphan removal
link:../../../../../test/java/org/hibernate/userguide/collections/BidirectionalBagOrphanRemovalTest.java[role=include]
link:extras/collections/collections-bidirectional-bag-orphan-removal-example.sql[role=include]

When rerunning the previous example, the child will get removed because the parent-side propagates the removal upon dissociating the child entity reference.

Ordered Lists

Although they use the List interface on the Java side, bags don’t retain element order. To preserve the collection element order, there are two possibilities:

@OrderBy

the collection is ordered upon retrieval using a child entity property

@OrderColumn

the collection uses a dedicated order column in the collection link table

Unidirectional ordered lists

When using the @OrderBy annotation, the mapping looks as follows:

Example 11. Unidirectional @OrderBy list
link:../../../../../test/java/org/hibernate/userguide/collections/UnidirectionalOrderedByListTest.java[role=include]

The database mapping is the same as with the Unidirectional bags example, so it won’t be repeated. Upon fetching the collection, Hibernate generates the following select statement:

Example 12. Unidirectional @OrderBy list select statement
link:extras/collections/collections-unidirectional-ordered-list-order-by-select-example.sql[role=include]

The child table column is used to order the list elements.

Note

The @OrderBy annotation can take multiple entity properties, and each property can take an ordering direction too (e.g. @OrderBy("name ASC, type DESC")).

If no property is specified (e.g. @OrderBy), the primary key of the child entity table is used for ordering.

Another ordering option is to use the @OrderColumn annotation:

Example 13. Unidirectional @OrderColumn list
link:../../../../../test/java/org/hibernate/userguide/collections/UnidirectionalOrderColumnListTest.java[role=include]
link:extras/collections/collections-unidirectional-ordered-list-order-column-example.sql[role=include]

This time, the link table takes the order_id column and uses it to materialize the collection element order. When fetching the list, the following select query is executed:

Example 14. Unidirectional @OrderColumn list select statement
link:extras/collections/collections-unidirectional-ordered-list-order-column-select-example.sql[role=include]

With the order_id column in place, Hibernate can order the list in-memory after it’s being fetched from the database.

Bidirectional ordered lists

The mapping is similar with the Bidirectional bags example, just that the parent side is going to be annotated with either @OrderBy or @OrderColumn.

Example 15. Bidirectional @OrderBy list
link:../../../../../test/java/org/hibernate/userguide/collections/BidirectionalOrderByListTest.java[role=include]

Just like with the unidirectional @OrderBy list, the number column is used to order the statement on the SQL level.

When using the @OrderColumn annotation, the order_id column is going to be embedded in the child table:

Example 16. Bidirectional @OrderColumn list
link:../../../../../test/java/org/hibernate/userguide/collections/BidirectionalOrderColumnListTest.java[role=include]
link:extras/collections/collections-bidirectional-ordered-list-order-column-example.sql[role=include]

When fetching the collection, Hibernate will use the fetched ordered columns to sort the elements according to the @OrderColumn mapping.

Customizing ordered list ordinal

You can customize the ordinal of the underlying ordered list by using the @ListIndexBase annotation.

Example 17. @ListIndexBase mapping example
link:../../../../../test/java/org/hibernate/userguide/collections/OrderColumnListIndexBaseTest.java[role=include]

When inserting two Phone records, Hibernate is going to start the List index from 100 this time.

Example 18. @ListIndexBase persist example
link:../../../../../test/java/org/hibernate/userguide/collections/OrderColumnListIndexBaseTest.java[role=include]
link:extras/collections/collections-customizing-ordered-list-ordinal-persist-example.sql[role=include]
Customizing ORDER BY SQL clause

While the JPA {jpaJavadocUrlPrefix}OrderBy.html[@OrderBy] annotation allows you to specify the entity attributes used for sorting when fetching the current annotated collection, the Hibernate specific @OrderBy annotation is used to specify a SQL clause instead.

In the following example, the @OrderBy annotation uses the CHAR_LENGTH SQL function to order the Article entities by the number of characters of the name attribute.

Example 19. @OrderBy mapping example
link:../../../../../test/java/org/hibernate/userguide/collections/OrderedBySQLTest.java[role=include]

When fetching the articles collection, Hibernate uses the ORDER BY SQL clause provided by the mapping:

Example 20. @OrderBy fetching example
link:../../../../../test/java/org/hibernate/userguide/collections/OrderedBySQLTest.java[role=include]
link:extras/collections/collections-customizing-ordered-by-sql-clause-fetching-example.sql[role=include]

Sets

Sets are collections that don’t allow duplicate entries and Hibernate supports both the unordered Set and the natural-ordering SortedSet.

Unidirectional sets

The unidirectional set uses a link table to hold the parent-child associations and the entity mapping looks as follows:

Example 21. Unidirectional set
link:../../../../../test/java/org/hibernate/userguide/collections/UnidirectionalSetTest.java[role=include]

The unidirectional set lifecycle is similar to that of the Unidirectional bags, so it can be omitted. The only difference is that Set doesn’t allow duplicates, but this constraint is enforced by the Java object contract rather than the database mapping.

Note

When using Sets, it’s very important to supply proper equals/hashCode implementations for child entities.

In the absence of a custom equals/hashCode implementation logic, Hibernate will use the default Java reference-based object equality which might render unexpected results when mixing detached and managed object instances.

Bidirectional sets

Just like bidirectional bags, the bidirectional set doesn’t use a link table, and the child table has a foreign key referencing the parent table primary key. The lifecycle is just like with bidirectional bags except for the duplicates which are filtered out.

Example 22. Bidirectional set
link:../../../../../test/java/org/hibernate/userguide/collections/BidirectionalSetTest.java[role=include]

Sorted sets

For sorted sets, the entity mapping must use the SortedSet interface instead. According to the SortedSet contract, all elements must implement the Comparable interface and therefore provide the sorting logic.

Unidirectional sorted sets

A SortedSet that relies on the natural sorting order given by the child element Comparable implementation logic might be annotated with the @SortNatural Hibernate annotation.

Example 23. Unidirectional natural sorted set
link:../../../../../test/java/org/hibernate/userguide/collections/UnidirectionalSortedSetTest.java[role=include]

The lifecycle and the database mapping are identical to the Unidirectional bags, so they are intentionally omitted.

To provide a custom sorting logic, Hibernate also provides a @SortComparator annotation:

Example 24. Unidirectional custom comparator sorted set
link:../../../../../test/java/org/hibernate/userguide/collections/UnidirectionalComparatorSortedSetTest.java[role=include]
Bidirectional sorted sets

The @SortNatural and @SortComparator work the same for bidirectional sorted sets too:

Example 25. Bidirectional natural sorted set
link:../../../../../test/java/org/hibernate/userguide/collections/BidirectionalSortedSetTest.java[role=include]

link:../../../../../test/java/org/hibernate/userguide/collections/UnidirectionalComparatorSortedSetTest.java[role=include]
Note

Before v6, @SortNatural must be used if collection element’s natural ordering is relied upon for sorting. Starting from v6, we can omit @SortNatural as it will take effect by default.

Maps

A java.util.Map is a ternary association because it requires a parent entity, a map key, and a value. An entity can either be a map key or a map value, depending on the mapping. Hibernate allows using the following map keys:

MapKeyColumn

for value type maps, the map key is a column in the link table that defines the grouping logic

MapKey

the map key is either the primary key or another property of the entity stored as a map entry value

MapKeyEnumerated

the map key is an Enum of the target child entity

MapKeyTemporal

the map key is a Date or a Calendar of the target child entity

MapKeyJoinColumn

the map key is an entity mapped as an association in the child entity that’s stored as a map entry key

Value type maps

A map of value type must use the @ElementCollection annotation, just like value type lists, bags or sets.

Example 26. Value type map with an entity as a map key
link:../../../../../test/java/org/hibernate/userguide/collections/ElementCollectionMapTest.java[role=include]
link:extras/collections/collections-map-value-type-entity-key-example.sql[role=include]

Adding entries to the map generates the following SQL statements:

Example 27. Adding value type map entries
link:../../../../../test/java/org/hibernate/userguide/collections/ElementCollectionMapTest.java[role=include]
link:extras/collections/collections-map-value-type-entity-key-add-example.sql[role=include]
Maps with a custom key type

Hibernate defines the @MapKeyType annotation which you can use to customize the Map key type.

Considering you have the following tables in your database:

link:extras/collections/collections-map-custom-key-type-sql-example.sql[role=include]

The call_register records the call history for every person. The call_timestamp_epoch column stores the phone call timestamp as a Unix timestamp since the Unix epoch.

Note

The @MapKeyColumn annotation is used to define the table column holding the key while the @Column mapping gives the value of the java.util.Map in question.

Since we want to map all the calls by their associated java.util.Date, not by their timestamp since epoch which is a number, the entity mapping looks as follows:

Example 28. @MapKeyType mapping example
link:../../../../../test/java/org/hibernate/userguide/collections/MapKeyTypeTest.java[role=include]

The associated TimestampEpochType looks as follows:

link:../../../../../test/java/org/hibernate/userguide/collections/type/TimestampEpochType.java[role=include]

The TimestampEpochType allows us to map a Unix timestamp since epoch to a java.util.Date. But, without the @MapKeyType Hibernate annotation, it would not be possible to customize the Map key type.

Maps having an interface type as the key

Considering you have the following PhoneNumber interface with an implementation given by the MobilePhone class type:

Example 29. PhoneNumber interface and the MobilePhone class type
link:../../../../../test/java/org/hibernate/userguide/collections/MapKeyClassTest.java[role=include]

If you want to use the PhoneNumber interface as a java.util.Map key, then you need to supply the {jpaJavadocUrlPrefix}MapKeyClass.html[@MapKeyClass] annotation as well.

Example 30. @MapKeyClass mapping example
link:../../../../../test/java/org/hibernate/userguide/collections/MapKeyClassTest.java[role=include]
link:extras/collections/collections-map-key-class-mapping-example.sql[role=include]

When inserting a Person with a callRegister containing 2 MobilePhone references, Hibernate generates the following SQL statements:

Example 31. @MapKeyClass persist example
link:../../../../../test/java/org/hibernate/userguide/collections/MapKeyClassTest.java[role=include]
link:extras/collections/collections-map-key-class-persist-example.sql[role=include]

When fetching a Person and accessing the callRegister Map, Hibernate generates the following SQL statements:

Example 32. @MapKeyClass fetch example
link:../../../../../test/java/org/hibernate/userguide/collections/MapKeyClassTest.java[role=include]
link:extras/collections/collections-map-key-class-fetch-example.sql[role=include]
Unidirectional maps

A unidirectional map exposes a parent-child association from the parent-side only.

The following example shows a unidirectional map which also uses a @MapKeyTemporal annotation. The map key is a timestamp, and it’s taken from the child entity table.

Note

The @MapKey annotation is used to define the entity attribute used as a key of the java.util.Map in question.

Example 33. Unidirectional Map
link:../../../../../test/java/org/hibernate/userguide/collections/UnidirectionalMapTest.java[role=include]
link:extras/collections/collections-map-unidirectional-example.sql[role=include]
Bidirectional maps

Like most bidirectional associations, this relationship is owned by the child-side while the parent is the inverse side and can propagate its own state transitions to the child entities.

In the following example, you can see that @MapKeyEnumerated was used so that the Phone enumeration becomes the map key.

Example 34. Bidirectional Map
link:../../../../../test/java/org/hibernate/userguide/collections/BidirectionalMapTest.java[role=include]
link:extras/collections/collections-map-bidirectional-example.sql[role=include]

Arrays

When discussing arrays, it is important to understand the distinction between SQL array types and Java arrays that are mapped as part of the application’s domain model.

Not all databases implement the SQL-99 ARRAY type and, for this reason, Hibernate doesn’t support native database array types.

Hibernate does support the mapping of arrays in the Java domain model - conceptually the same as mapping a List. However, it is important to realize that it is impossible for Hibernate to offer lazy-loading for arrays of entities and, for this reason, it is strongly recommended to map a "collection" of entities using a List rather than an array.

Arrays as binary

By default, Hibernate will choose a BINARY type, as supported by the current Dialect.

Example 35. Arrays stored as binary
link:../../../../../test/java/org/hibernate/userguide/collections/ArrayTest.java[role=include]
link:extras/collections/collections-array-binary-example.sql[role=include]
Note

If you want to map arrays such as String[] or int[] to database-specific array types like PostgreSQL integer[] or text[], you need to write a custom Hibernate Type.

Check out this article for an example of how to write such a custom Hibernate Type.

Collections as basic value type

Notice how all the previous examples explicitly mark the collection attribute as either ElementCollection, OneToMany or ManyToMany. Collections not marked as such require a custom Hibernate Type and the collection elements must be stored in a single database column.

This is sometimes beneficial. Consider a use-case such as a VARCHAR column that represents a delimited list/set of Strings.

Example 36. Comma delimited collection
link:../../../../../test/java/org/hibernate/userguide/collections/BasicTypeCollectionTest.java[role=include]

link:../../../../../test/java/org/hibernate/userguide/collections/type/CommaDelimitedStringsJavaTypeDescriptor.java[role=include]

link:../../../../../test/java/org/hibernate/userguide/collections/type/CommaDelimitedStringsType.java[role=include]

The developer can use the comma-delimited collection like any other collection we’ve discussed so far and Hibernate will take care of the type transformation part. The collection itself behaves like any other basic value type, as its lifecycle is bound to its owner entity.

Example 37. Comma delimited collection lifecycle
link:../../../../../test/java/org/hibernate/userguide/collections/BasicTypeCollectionTest.java[role=include]
link:extras/collections/collections-comma-delimited-collection-lifecycle-example.sql[role=include]

See the Hibernate Integrations Guide for more details on developing custom value type mappings.

Custom collection types

If you wish to use other collection types than List, Set or Map, like Queue for instance, you have to use a custom collection type, as illustrated by the following example:

Example 38. Custom collection mapping example
link:../../../../../test/java/org/hibernate/userguide/collections/QueueTest.java[role=include]

link:../../../../../test/java/org/hibernate/userguide/collections/type/QueueType.java[role=include]

link:../../../../../test/java/org/hibernate/userguide/collections/type/PersistentQueue.java[role=include]
Note

The reason why the Queue interface is not used for the entity attribute is because Hibernate only allows the following types:

  • java.util.List

  • java.util.Set

  • java.util.Map

  • java.util.SortedSet

  • java.util.SortedMap

However, the custom collection type can still be customized as long as the base type is one of the aforementioned persistent types.

This way, the Phone collection can be used as a java.util.Queue:

Example 39. Custom collection example
link:../../../../../test/java/org/hibernate/userguide/collections/QueueTest.java[role=include]