3.2.5.3. Collections
3.2.5.3.1. Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can map
Collection, List (ie ordered lists, not indexed lists), Map and Set. The EJB3 specification describes how to map an ordered list (ie a list ordered at load time) using @javax.persistence.OrderBy annotation: this annotation takes into parameter a list of comma separated (target entity) properties to order the collection by (eg firstname asc, age desc), if the string is empty, the collection will be ordered by id.
Note
When
@OrderBy is used on joined classes (using a join table), the generated SQL is invalid on MySQL, PostgreSQL, Oracle, and Microsoft SQL, because the order by clause qualifies the columns using the actual table name. The order by clause should use the table alias instead.
For true indexed collections, please refer to the Section 3.4, “Hibernate Annotation Extensions”. EJB3 allows you to map Maps using as a key one of the target entity property using
@MapKey(name="myProperty") (myProperty is a property name in the target entity). When using @MapKey (without property name), the target entity primary key is used. The map key uses the same column as the property pointed out: there is no additional column defined to hold the map key, and it does make sense since the map key actually represent a target property. Be aware that once loaded, the key is no longer kept in sync with the property, in other words, if you change the property value, the key will not change automatically in your Java model (for true map support please refers to Section 3.4, “Hibernate Annotation Extensions”). Many people confuse <map> capabilities and @MapKey ones. These are two different features. @MapKey still has some limitations, please check the forum or the JIRA tracking system for more informations.
Hibernate has several notions of collections.
| Semantic | java representation | annotations |
|---|---|---|
| Bag semantic |
java.util.List
java.util.Collection
|
@org.hibernate.annotations.CollectionOfElements
or @OneToMany
or @ManyToMany
|
| Bag semantic with primary key (without the limitations of Bag semantic) | java.util.List, java.util.Collection | (@org.hibernate.annotations.CollectionOfElements or @OneToMany or @ManyToMany) and @CollectionId |
| List semantic | java.util.List | (@org.hibernate.annotations.CollectionOfElements or @OneToMany or @ManyToMany) and @org.hibernate.annotations.IndexColumn |
| Set semantic | java.util.Set | @org.hibernate.annotations.CollectionOfElements or @OneToMany or @ManyToMany |
| Map semantic | java.util.Map | (@org.hibernate.annotations.CollectionOfElements or @OneToMany or @ManyToMany) and (nothing or @org.hibernate.annotations.MapKey/MapKeyManyToMany for true map support, OR @javax.persistence.MapKey |
Collection of primitive, core type or embedded objects is not supported by the EJB3 specification. Hibernate Annotations allows them however (see Section 3.4, “Hibernate Annotation Extensions”).
So
City has a collection of Streets that are ordered by streetName (of Street) when the collection is loaded. Software has a map of Versions which key is the Version codeName.
Unless the collection is a generic, you will have to define
targetEntity. This is a annotation attribute that take the target entity class as a value.