组件(Component)映射

来源:互联网 发布:淘宝店描述怎么写 编辑:程序博客网 时间:2024/05/16 15:59

第 9 章 组件(Component)映射

Component这个概念在Hibernate中几处不同的地方为了不同的目的被重复使用.

9.1. 依赖对象(Dependent objects)

Component是一个被包含的对象,它作为值类型被持久化,而非一个被引用的实体。“component(组件)”这一术语指的是面向对象的合成概念(而并不是系统构架层次上的组件的概念)举个例子, 你可以对人(Person)如以下这样来建模:

public class Person {    private java.util.Date birthday;    private Name name;    private String key;    public String getKey() {        return key;    }    private void setKey(String key) {        this.key=key;    }    public java.util.Date getBirthday() {        return birthday;    }    public void setBirthday(java.util.Date birthday) {        this.birthday = birthday;    }    public Name getName() {        return name;    }    public void setName(Name name) {        this.name = name;    }    ......    ......}
public class Name {    char initial;    String first;    String last;    public String getFirst() {        return first;    }    void setFirst(String first) {        this.first = first;    }    public String getLast() {        return last;    }    void setLast(String last) {        this.last = last;    }    public char getInitial() {        return initial;    }    void setInitial(char initial) {        this.initial = initial;    }}

现在,姓名(Name)是作为人(Person)的一个组成部分。需要注意的是:需要对姓名的持久化属性定义getter和setter方法,但是不需要实现任何的接口或申明标识符字段。

以下是这个例子的Hibernate映射文件:

<class name="eg.Person" table="person">    <id name="Key" column="pid" type="string">        <generator class="uuid.hex"/>    </id>    <property name="birthday" type="date"/>    <component name="Name" class="eg.Name"> <!-- class attribute optional -->        <property name="initial"/>        <property name="first"/>        <property name="last"/>    </component></class>

人员(Person)表中将包括pid,birthday,initial, firstlast等字段。

就像所有的值类型一样, Component不支持共享引用。 换句话说,两个人可能重名,但是两个person对象应该包含两个独立的name对象,只不过是具有“同样”的值。Component的值为空从语义学上来讲是专有的(ad hoc)。 每当重新加载一个包含组件的对象,如果component的所有字段为空,那么将Hibernate将假定整个component为空。对于绝大多数目的,这样假定是没有问题的。

Component的属性可以是Hibernate类型(包括Collections, many-to-one 关联, 以及其它Component 等等)。嵌套Component不应该作为特殊的应用被考虑(Nested components should not be considered an exotic usage)。 Hibernate趋向于支持设计细致(fine-grained)的对象模型。

<component> 元素还允许有 <parent>子元素 ,用来表明component类中的一个属性返回包含它的实体的引用。

<class name="eg.Person" table="person">    <id name="Key" column="pid" type="string">        <generator class="uuid.hex"/>    </id>    <property name="birthday" type="date"/>    <component name="Name" class="eg.Name" unique="true">>        <parent name="namedPerson"/> <!-- reference back to the Person -->        <property name="initial"/>        <property name="first"/>        <property name="last"/>    </component></class>

9.2. 在集合中出现的依赖对象

Hibernate支持component的集合(例如: 一个元素是“姓名”这种类型的数组)。 你可以使用<composite-element>标签替代<element>标签来定义你的component集合。

<set name="someNames" table="some_names" lazy="true">    <key column="id"/>    <composite-element class="eg.Name"> <!-- class attribute required -->        <property name="initial"/>        <property name="first"/>        <property name="last"/>    </composite-element></set>

注意,如果你决定定义一个元素是联合元素的Set,正确地实现equals()hashCode()是非常重要的。

组合元素可以包含component但是不能包含集合。如果你的组合元素自身包含component, 必须使用<nested-composite-element>标签。这是一个相当特殊的案例 - 组合元素的集合自身可以包含component。 这个时候你就应该考虑一下使用one-to-many关联是否会更恰当。 尝试对这个组合元素重新建模为一个实体-但是需要注意的是,虽然Java模型和重新建模前 是一样的,关系模型和持久性语义上仍然存在轻微的区别。

请注意如果你使用<set>标签,一个组合元素的映射不支持可能为空的属性. 当删除对象时, Hibernate必须使用每一个字段的来确定一条记录(在组合元素表中,没有单个的关键字段), 如果有为null的字段,这样做就不可能了。你必须作出一个选择,要么在组合元素中使用不能为空的属性, 要么选择使用<list>,<map>,<bag> 或者 <idbag>而不是 <set>

组合元素有个特别的案例,是组合元素可以包含一个<many-to-one> 元素。类似这样的映射允许你映射一个many-to-mang关联表作为组合元素额外的字段。(A mapping like this allows you to map extra columns of a many-to-many association table to the composite element class.) 接下来的的例子是从OrderItem的一个多对多的关联关系,而purchaseDate, pricequantityItem的关联属性。

<class name="eg.Order" .... >    ....    <set name="purchasedItems" table="purchase_items" lazy="true">        <key column="order_id">        <composite-element class="eg.Purchase">            <property name="purchaseDate"/>            <property name="price"/>            <property name="quantity"/>            <many-to-one name="item" class="eg.Item"/> <!-- class attribute is optional -->        </composite-element>    </set></class>

当然,在另一方面,无法存在指向purchase的关联,因此不能实现双向关联查询。记住组建是值类型,并且不允许共享关联。单个Purchase 可以放在包含Order的集合中,但它不能同时被Item所关联。

即使三重或多重管理都是可能的:

<class name="eg.Order" .... >    ....    <set name="purchasedItems" table="purchase_items" lazy="true">        <key column="order_id">        <composite-element class="eg.OrderLine">            <many-to-one name="purchaseDetails" class="eg.Purchase"/>            <many-to-one name="item" class="eg.Item"/>        </composite-element>    </set></class>

在查询中,组合元素使用的语法是和关联到其他实体的语法一样的。

9.3. 组件作为Map的索引(Components as Map indices )

<composite-map-key>元素允许你映射一个Component类作为Map的key, 但是你必须确定你正确的在这个类中重写了hashCode()equals()方法。

9.4. 组件作为联合标识符(Components as composite identifiers)

你可以使用一个component作为一个实体类的标识符。 你的component类必须满足以下要求:

  • 它必须实现java.io.Serializable接口

  • 它必须重新实现equals()hashCode()方法, 始终和组合关键字在数据库中的概念保持一致

注意:在Hibernate3中,第二种要求并非是Hibernate强制必须的。但最好这样做。

你不能使用一个IdentifierGenerator产生组合关键字。作为替代应用程序必须分配它自己的标识符。

使用<composite-id> 标签(并且内嵌<key-property>元素)代替通常的<id>标签。 比如,OrderLine类具有一个依赖Order的(联合)主键的主键。

<class name="OrderLine">        <composite-id name="id" class="OrderLineId">        <key-property name="lineId"/>        <key-property name="orderId"/>        <key-property name="customerId"/>    </composite-id>        <property name="name"/>        <many-to-one name="order" class="Order"            insert="false" update="false">        <column name="orderId"/>        <column name="customerId"/>    </many-to-one>    ....    </class>

现在,任何关联到OrderLine 的外键都是复合的。在你的映射文件中,必须为其他类也这样声明。指向OrderLine的关联可能被这样映射:

<many-to-one name="orderLine" class="OrderLine"><!-- the "class" attribute is optional, as usual -->    <column name="lineId"/>    <column name="orderId"/>    <column name="customerId"/></many-to-one>

(注意在各个地方<column>标签都是column属性的替代写法。)

指向OrderLine多对多关联也使用联合外键:

<set name="undeliveredOrderLines">    <key column name="warehouseId"/>    <many-to-many class="OrderLine">        <column name="lineId"/>        <column name="orderId"/>        <column name="customerId"/>    </many-to-many></set>

Order中, OrderLine的集合则是这样:

<set name="orderLines" inverse="true">    <key>        <column name="orderId"/>        <column name="customerId"/>    </key>    <one-to-many class="OrderLine"/></set>

(与通常一样,<one-to-many>元素不声明任何列.)

假若OrderLine本身拥有一个集合,它也具有组合外键。

<class name="OrderLine">    ....    ....    <list name="deliveryAttempts">        <key>   <!-- a collection inherits the composite key type -->            <column name="lineId"/>            <column name="orderId"/>            <column name="customerId"/>        </key>        <list-index column="attemptId" base="1"/>        <composite-element class="DeliveryAttempt">            ...        </composite-element>    </set></class>

9.5. 动态组件 (Dynamic components)

你甚至可以映射Map类型的属性:

<dynamic-component name="userAttributes">    <property name="foo" column="FOO"/>    <property name="bar" column="BAR"/>    <many-to-one name="baz" class="Baz" column="BAZ_ID"/></dynamic-component>

<dynamic-component>映射的语义上来讲,它和<component>是相同的。这种映射类型的优点在于通过修改映射文件,就可以具有在部署时检测真实属性的能力.利用一个DOM解析器,是有可能在运行时刻操作映射文件的。 更好的是,你可以通过Configuration对象来访问(或者修改)Hibernate的运行时元模型。

0 0
原创粉丝点击