‘Chapiar 7. Collection manping
Chapter 7. Collection mapping
7.2, Persistent colections
7.2. Mow to map collections
7.2.1. Collection forelgn keys
7.2.2. Indexed collections
7.2.3, Collections of basic types and embeddable objects
7.3. Advanced collection mappings
7.3.1. Sorted collections
7.3.2. Biciectional associations
7.313. Bidirectional associations with Indexed collecions
7.34. Temary associations
7:3:5.Using an
7.4, Collection examples
7.1, Persistent collections
Naturally Hivemate also allows to persist collections. These persistent collections can contain almost
any other Hibernate type, Including: basic types, custom types, components and references to other
entities. The distinction between value and reference semantics Is in this context very important. An
object in a collecion might be handled with "value" semantics (ts Ife cyde fully depencs on the
collection owner), or t might be a reference to anather entity with its own lifecycle. In the latter case,
only the "ink" between the two objects is considered to be a state held by the collection.
[As 2 requirement persistent collectionvalued fields must be dedared as an interface type (see
Example 7.2, "Collection mapping using @OneToMany and @JoinColumn") The actual interface might be
Jova.ut.Set, java Collection, java. List, Jove.ut. Map, java. SortedSe, Jave.utlSortectsp of anything
you tke ("anything you ke" means you will have to write an implementation of
org ibernate.userype.UserClectionType)
Notice how in Example 7.2, "Collection mapping using @OneToMany and @2oinColumn” the instance
variable pers was intialzed with an instance of HasnSet. Ths fs the best way to Intialze collection
valuee properties of newly instantiated (non-persistent) instances, When you make the instance
persistent, by calling persist), Hibernate will acually replace the HeshSet with an instance of Hibernate's
‘own implementation of Se. Be aware of the folowing error
‘The persistent collections injected by Hibernate behave tke HashMap, HashSet, TreeMap, TreeSet oF
Arrayust, depending on the interface type,
Collections instances have the usual behavior of value types. They are automatically persisted when
oes jboss rgihibernalorn/.Brlerencolen-UStmlcoectons Hint
+120seas ‘Chapiar 7. Collection manping
referenced by a persistent object and are automatially deleted when unreferenced. Ifa collecton is
passed fromone persistent object to another, its elements might be moved from one table to another.
‘Two entities cannot share a reference to the same collacton instance. Que to the underiing relational
‘model, eollection-valued properties do not support null value semantics. Hibernate does not distinguish
between a nul collection reference and an empty collection.
Note
Use persistent
su Understand the semantics of bid
“igrectional associations")
7.2. How to map collect
ns
Using annotations you can map Collections, Lists, Maps and Sets of associated entities using
@OnetoMany and @ManyToMany. For collecvons of a basic or embeddable type use
(@ElementCollection. In the simplest case a collection mapping looks like this:
Example 7.2. Collection mapping using @OneToMany and @3oinColumn
public SetcPare> get2arts(} | return parte:
testeey
past |
Product describes a unidirectional relationship with Part using the join colums PART_ID. In this
Unidirectional one to many scenario you can also use a join table as seen in Example 7.3, “Collection
‘mapping using @OneTeMany anc @loinTable”
Example 7.3. Collection mapping using @OneToMany and @JoinTable
public clans eect |
oes jboss rgihibernalorn/.Brlerencolen-UStmlcoectons Hintseas
‘Chapiar 7. Collection manping
Without describing any physical mapping (no ioinColuma or @uoinTable), a unidirectional one to many
with Join table is used. The table name isthe concatenation of the owner table name, _, and the other
side table name. The foreign key name(s) referencing the owner table is the concatenation of the owner
able, _, and the owner primary key colurm(s) name, The foreign key name(s) referencing the other side
's the concatenation ofthe owner property name, _, and the other side primary key colurmn(s) name. A
‘unique constraint fs added to the foreign key referencing the other side table to reflec the one to many,
Lets have 2 look now how collections are mapped using Hibernate mapping les, In this case the fst
step is to chose the right mapping element. It depencs on the type of interface. For example, a
Clement Is used for mapping properties of type Set
Example 7.4. Mapping a Set using
In Example 7.4, “Mapping @ Set Using " @ one-to-many association links the Product and Part
lentes. Ths association requires the existence of a foreign key column and possibly an index column to
the Part table. This mapping loses certain semantics of normal Java collections:
» An instance of te contained entity dass cannot belong to more than one instance ofthe collection.
> An instance of the contained ently class cannot appear at more than one value of the collection
Index.
Looking doser atthe used tag we see that It has the following options.
Example 7.5. options of element
lass (Fequired): the name of the assocated dass
not~ound (optional - defaults to exception): species how cached identifies that reference
rmssing rows willbe handled. anor will treat a missing row as a null association,
enity-name (optional): the entity name of the associated class, as an alternative to class
The element Goes not need to declare any colurms. Nor is it necessary to specify the
‘able name anywhere.
Warning
column jon is dedared No
‘rue’, See Section 7.3.2, ‘Bidirectional associations
‘Apart from the tag as shown in Example 7.4, "Mapping @ Set using ", there is also ,
-