6.2. 集合映射( Collection mappings )

提示

任何值集合或者多对多关联需要专用的具有一个或多个外键字段的collection table、一个或多个collection element column,以及还可能有一个或多个索引字段。

用于映射集合类的Hibernate映射元素取决于接口的类型。比如, <set> 元素用来映射Set类型的属性。

<class name="Product">
    <id name="serialNumber" column="productSerialNumber"/>
    <set name="parts">
        <key column="productSerialNumber" not-null="true"/>
        <one-to-many class="Part"/>
    </set>
</class>

除了<set>,还有<list>, <map>, <bag>, <array><primitive-array> 映射元素。<map>具有代表性:

<map
    name="propertyName"                                         (1)
    table="table_name"                                          (2)
    schema="schema_name"                                        (3)
    lazy="true|extra|false"                                     (4)
    inverse="true|false"                                        (5)
    cascade="all|none|save-update|delete|all-delete-orphan|delet(6)e-orphan"
    sort="unsorted|natural|comparatorClass"                     (7)
    order-by="column_name asc|desc"                             (8)
    where="arbitrary sql where condition"                       (9)
    fetch="join|select|subselect"                               (10)
    batch-size="N"                                              (11)
    access="field|property|ClassName"                           (12)
    optimistic-lock="true|false"                                (13)
    mutable="true|false"                                        (14)
    node="element-name|."
    embed-xml="true|false"
>

    <key .... />
    <map-key .... />
    <element .... />
</map>
(1)

name 集合属性的名称

(2)

table (可选——默认为属性的名称)这个集合表的名称(不能在一对多的关联关系中使用)

(3)

schema (可选) 表的schema的名称, 他将覆盖在根元素中定义的schema

(4)

lazy (可选--默认为true) 可以用来关闭延迟加载(false),指定一直使用预先抓取,或者打开"extra-lazy" 抓取,此时大多数操作不会初始化集合类(适用于非常大的集合)

(5)

inverse (可选——默认为false) 标记这个集合作为双向关联关系中的方向一端。

(6)

cascade (可选——默认为none) 让操作级联到子实体

(7)

sort(可选)指定集合的排序顺序, 其可以为自然的(natural)或者给定一个用来比较的类。

(8)

order-by (可选, 仅用于jdk1.4) 指定表的字段(一个或几个)再加上asc或者desc(可选), 定义Map,Set和Bag的迭代顺序

(9)

where (可选) 指定任意的SQL where条件, 该条件将在重新载入或者删除这个集合时使用(当集合中的数据仅仅是所有可用数据的一个子集时这个条件非常有用)

(10)

fetch (可选, 默认为select) 用于在外连接抓取、通过后续select抓取和通过后续subselect抓取之间选择。

(11)

batch-size (可选, 默认为1) 指定通过延迟加载取得集合实例的批处理块大小("batch size")。

(12)

access(可选-默认为属性property):Hibernate取得集合属性值时使用的策略

(13)

乐观锁 (可选 - 默认为 true): 对集合的状态的改变会是否导致其所属的实体的版本增长。 (对一对多关联来说,关闭这个属性常常是有理的)

(14)

mutable(可变) (可选 - 默认为true): 若值为false,表明集合中的元素不会改变(在某些情况下可以进行一些小的性能优化)。

6.2.1. 集合外键(Collection foreign keys)

集合实例在数据库中依靠持有集合的实体的外键加以辨别。此外键作为集合关键字段(collection key column)(或多个字段)加以引用。集合关键字段通过<key> 元素映射。

在外键字段上可能具有非空约束。对于大多数集合来说,这是隐含的。对单向一对多关联来说,外键字段默认是可以为空的,因此你可能需要指明 not-null="true"

<key column="productSerialNumber" not-null="true"/>

外键约束可以使用ON DELETE CASCADE

<key column="productSerialNumber" on-delete="cascade"/>

<key> 元素的完整定义,请参阅前面的章节。

6.2.2. 集合元素(Collection elements)

集合几乎可以包含任何其他的Hibernate类型,包括所有的基本类型、自定义类型、组件,当然还有对其他实体的引用。存在一个重要的区别:位于集合中的对象可能是根据“值”语义来操作(其声明周期完全依赖于集合持有者),或者它可能是指向另一个实体的引用,具有其自己的生命周期。在后者的情况下,被作为集合持有的状态考虑的,只有两个对象之间的“连接”。

被包容的类型被称为集合元素类型(collection element type)。集合元素通过<element><composite-element>映射,或在其是实体引用的时候,通过<one-to-many><many-to-many>映射。前两种用于使用值语义映射元素,后两种用于映射实体关联。

6.2.3. 索引集合类(Indexed collections)

所有的集合映射,除了set和bag语义的以外,都需要指定一个集合表的索引字段(index column)——用于对应到数组索引,或者List的索引,或者Map的关键字。通过<map-key>,Map 的索引可以是任何基础类型;若通过<map-key-many-to-many>,它也可以是一个实体引用;若通过<composite-map-key>,它还可以是一个组合类型。数组或列表的索引必须是integer类型,并且使用 <list-index>元素定义映射。被映射的字段包含有顺序排列的整数(默认从0开始)。

<list-index 
        column="column_name"                (1)
        base="0|1|..."/>
(1)

column_name (required): The name of the column holding the collection index values.

(1)

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"                (1)
        formula="any SQL expression"        (2)
        type="type_name"                    (3)
        node="@attribute-name"
        length="N"/>
(1)

column (optional): The name of the column holding the collection index values.

(2)

formula (optional): A SQL formula used to evaluate the key of the map.

(3)

type (reguired): The type of the map keys.

<map-key-many-to-many
        column="column_name"                (1)
        formula="any SQL expression"        (2)(3)
        class="ClassName"
/>
(1)

column (optional): The name of the foreign key column for the collection index values.

(2)

formula (optional): A SQL formula used to evaluate the foreign key of the map key.

(3)

class (required): The entity class used as the map key.

值集合于多对多关联(Collections of values and many-to-many associations)

6.2.4. 对于一个值集合, 我们使用<element>标签。

column(可选):保存集合元素值的字段名。

formula (可选): 用于计算元素的SQL公式

<element
        column="column_name"                     (1)
        formula="any SQL expression"             (2)
        type="typename"                          (3)
        length="L"
        precision="P"
        scale="S"
        not-null="true|false"
        unique="true|false"
        node="element-name"
/>
(1)

column (optional): The name of the column holding the collection element values.

(2)

formula (optional): An SQL formula used to evaluate the element.

(3)

type (required): The type of the collection element.

A many-to-many association is specified using the <many-to-many> element.

<many-to-many
        column="column_name"                               (1)
        formula="any SQL expression"                       (2)
        class="ClassName"                                  (3)
        fetch="select|join"                                (4)
        unique="true|false"                                (5)
        not-found="ignore|exception"                       (6)
        entity-name="EntityName"                           (7)
        property-ref="propertyNameFromAssociatedClass"     (8)
        node="element-name"
        embed-xml="true|false"
    />
(1)

column (optional): The name of the element foreign key column.

(2)

formula (optional): An SQL formula used to evaluate the element foreign key value.

(3)

class (required): The name of the associated class.

(4)

fetch (optional - defaults to join): enables outer-join or sequential select fetching for this association. This is a special case; for full eager fetching (in a single SELECT) of an entity and its many-to-many relationships to other entities, you would enable join fetching not only of the collection itself, but also with this attribute on the <many-to-many> nested element.

(5)

unique (optional): Enable the DDL generation of a unique constraint for the foreign-key column. This makes the association multiplicity effectively one to many.

(6)

not-found (optional - defaults to exception): Specifies how foreign keys that reference missing rows will be handled: ignore will treat a missing row as a null association.

(7)

entity-name (optional): The entity name of the associated class, as an alternative to class.

(8)

property-ref: (optional) The name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.

包含一组整数的bag(还设置了order-by参数指定了迭代的顺序):

<set name="names" table="person_names">
    <key column="person_id"/>
    <element column="person_name" type="string"/>
</set>

一个实体数组,在这个案例中是一个多对多的关联(注意这里的实体是自动管理生命周期的对象(life cycle objects),cascade="all"):

<bag name="sizes" 
        table="item_sizes" 
        order-by="size asc">
    <key column="item_id"/>
    <element column="size" type="integer"/>
</bag>

一个map,通过字符串的索引来指明日期:

<array name="addresses" 
        table="PersonAddress" 
        cascade="persist">
    <key column="personId"/>
    <list-index column="sortOrder"/>
    <many-to-many column="addressId" class="Address"/>
</array>

一个组件的列表:(下一章讨论)

<map name="holidays" 
        table="holidays" 
        schema="dbo" 
        order-by="hol_name asc">
    <key column="id"/>
    <map-key column="hol_name" type="string"/>
    <element column="hol_date" type="date"/>
</map>

一对多关联(One-to-many Associations)

<list name="carComponents" 
        table="CarComponents">
    <key column="carId"/>
    <list-index column="sortOrder"/>
    <composite-element class="CarComponent">
        <property name="price"/>
        <property name="type"/>
        <property name="serialNumber" column="serialNum"/>
    </composite-element>
</list>

6.2.5. 一对多关联通过外键连接两个类对应的表,而没有中间集合表。 这个关系模型失去了一些Java集合的语义:

一个被包含的实体的实例只能被包含在一个集合的实例中

  • 一个被包含的实体的实例只能对应于集合索引的一个值中

  • 一个从ProductPart的关联需要关键字字段,可能还有一个索引字段指向Part所对应的表。 <one-to-many>标记指明了一个一对多的关联。

class(必须):被关联类的名称。

<one-to-many 
        class="ClassName"                                  (1)
        not-found="ignore|exception"                       (2)
        entity-name="EntityName"                           (3)
        node="element-name"
        embed-xml="true|false"
    />
(1)

class (required): The name of the associated class.

(2)

entity-name (可选): 被关联的类的实体名,作为class的替代。

(3)

entity-name (optional): The entity name of the associated class, as an alternative to class.

注意:<one-to-many>元素不需要定义任何字段。 也不需要指定表名。

重要提示:如果一对多关联中的外键字段定义成NOT NULL,你必须把<key>映射声明为not-null="true",或者使用双向关联,并且标明inverse="true"。参阅本章后面关于双向关联的讨论。

下面的例子展示一个Part实体的map,把name作为关键字。( partNamePart的持久化属性)。注意其中的基于公式的索引的用法。

<map name="parts"
        cascade="all">
    <key column="productId" not-null="true"/>
    <map-key formula="partName"/>
    <one-to-many class="Part"/>
</map>