Menu Close
Settings Close

Language and Page Formatting Options

6.2. Collection mappings

The Hibernate mapping element used for mapping a collection depends upon the type of the interface. For example, a <set> element is used for mapping properties of type Set.
<class name="Product">
    <id name="serialNumber" column="productSerialNumber"/>
    <set name="parts">
        <key column="productSerialNumber" not-null="true"/>
        <one-to-many class="Part"/>
Apart from <set>, there is also <list>, <map>, <bag>, <array> and <primitive-array> mapping elements. The <map> element is representative:
    order-by="column_name asc|desc"
    where="arbitrary sql where condition"

    <key .... />
    <map-key .... />
    <element .... />
name the collection property name
table (optional - defaults to property name) the name of the collection table (not used for one-to-many associations)
schema (optional) the name of a table schema to override the schema declared on the root element
lazy (optional - defaults to true) may be used to disable lazy fetching and specify that the association is always eagerly fetched, or to enable "extra-lazy" fetching where most operations do not initialize the collection (suitable for very large collections)
inverse (optional - defaults to false) mark this collection as the "inverse" end of a bidirectional association
cascade (optional - defaults to none) enable operations to cascade to child entities
sort (optional) specify a sorted collection with natural sort order, or a given comparator class
order-by (optional, JDK1.4 only) specify a table column (or columns) that define the iteration order of the Map, Set or bag, together with an optional asc or desc
where (optional) specify an arbitrary SQL WHERE condition to be used when retrieving or removing the collection (useful if the collection should contain only a subset of the available data)
fetch (optional, defaults to select) Choose between outer-join fetching, fetching by sequential select, and fetching by sequential subselect.
batch-size (optional, defaults to 1) specify a "batch size" for lazily fetching instances of this collection.
access (optional - defaults to property): The strategy Hibernate should use for accessing the collection property value.
optimistic-lock (optional - defaults to true): Species that changes to the state of the collection results in increment of the owning entity's version. (For one to many associations, it is often reasonable to disable this setting.)
mutable (optional - defaults to true): A value of false specifies that the elements of the collection never change (a minor performance optimization in some cases).

6.2.1. Collection foreign keys

Collection instances are distinguished in the database by the foreign key of the entity that owns the collection. This foreign key is referred to as the collection key column (or columns) of the collection table. The collection key column is mapped by the <key> element.
There may be a nullability constraint on the foreign key column. For most collections, this is implied. For unidirectional one to many associations, the foreign key column is nullable by default, so you might need to specify not-null="true".
<key column="productSerialNumber" not-null="true"/>
The foreign key constraint may use ON DELETE CASCADE.
<key column="productSerialNumber" on-delete="cascade"/>
See the previous chapter for a full definition of the <key> element.