6.2.3. Indexed collections
All collection mappings, except those with set and bag semantics, need an
index column
in the collection table - a column that maps to an array index, or List
index, or Map
key. The index of a Map
may be of any basic type, mapped with <map-key>
, it may be an entity reference mapped with <map-key-many-to-many>
, or it may be a composite type, mapped with <composite-map-key>
. The index of an array or list is always of type integer
and is mapped using the <list-index>
element. The mapped column contains sequential integers (numbered from zero, by default).
<list-index
column="column_name"
base="0|1|..."/>
column_name
(required): The name of the column holding the collection index values.
base
(optional, defaults to 0
): The value of the index column that corresponds to the first element of the list or array.
<map-key
column="column_name"
formula="any SQL expression"
type="type_name"
node="@attribute-name"
length="N"/>
column
(optional): The name of the column holding the collection index values.
formula
(optional): A SQL formula used to evaluate the key of the map.
type
(reguired): The type of the map keys.
<map-key-many-to-many
column="column_name"
formula="any SQL expression"
class="ClassName"
/>
column
(optional): The name of the foreign key column for the collection index values.
formula
(optional): A SQL formula used to evaluate the foreign key of the map key.
class
(required): The entity class used as the map key.
If your table doesn't have an index column, and you still wish to use List
as the property type, you should map the property as a Hibernate
<bag>
. A bag does not retain its order when it is retrieved from the database, but it may be optionally sorted or ordered.
There are quite a range of mappings that can be generated for collections, covering many common relational models. We suggest you experiment with the schema generation tool to get a feeling for how various mapping declarations translate to database tables.