Hibernate组件映射

组件Components

除了粗粒度的对象模型设计(一个表映射成一个持久化类)之外,还可以采用细粒度的对象模型,把一个表的映射成两个或者多个类。

被细化出来的类,可以称为组件(Component)

组件映射

组件映射的单向关联

组件映射的双向关联

组件集合映射 

使用组件集合,可以让组件对象的集合依附于一个持久化对象上

 

本来说自己写的,网上看了看资料太全面了  就收集整理好了

 

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

依赖对象(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不支持共享引用。Component的值为空从语义学上来讲是专有的。 每当 重新加载一个包含组件的对象,如果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">  
        <parent name="namedPerson"/> <!-- reference back to the Person -->  
        <property name="initial"/>  
        <property name="first"/>  
        <property name="last"/>  
    </component>  
</class>

 

在集合中出现的依赖对象

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> 

 

即使三重或多重管理都是可能的:
<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>  
 

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

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

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

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

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

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

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

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

既然联合标识符必须在对象存储之前被分配,我们就不能使用标识符的unsaved-value来把刚刚新建的实例和在先前的session保存的实例区分开来。

如果你希望使用saveOrUpdate()或者级联保存/更新(cascading save / update),你应该实现 Interceptor.isUnsaved()。使用<composite-id> 标签(它和<component>标签有同样的属性和元素)代替<id>标签。 下面有个联合标识符类的定义:

<class name="eg.Foo" table"FOOS">  
    <composite-id name="compId" class="eg.FooCompositeID">  
        <key-property name="string"/>  
        <key-property name="short"/>  
        <key-property name="date" column="date_" type="date"/>  
    </composite-id>  
    <property name="name"/>  
    ....   
</class>

 

这时候,任何到FOOS的外键也同样是联合的, 在你其他类的映射文件中也必须同样定义。 一个到Foo的定义应该像以下这样:

<many-to-one name="foo" class="eg.Foo">  
<!-- the "class" attribute is optional, as usual -->  
    <column name="foo_string"/>  
    <column name="foo_short"/>  
    <column name="foo_date"/>  
</many-to-one> 

 

新的 <column> 标签同样被用于包含多个字段的自定义类型(This new columntag is also used by multi-column custom types)。 事实上在各个地方它都是一个可选的字段属性。要定义一个元素是 Foo的集合类,要这样写:
<set name="foos">  
    <key column="owner_id"/>  
    <many-to-many class="eg.Foo">  
        <column name="foo_string"/>  
        <column name="foo_short"/>  
        <column name="foo_date"/>  
    </many-to-many>  
</set> 
 

另一方面,<one-to-many>元素通常不定义字段.

如果 Foo 自己包含集合, 那么他们也需要使用联合外键。

<class name="eg.Foo">  
    ....   
    ....   
    <set name="dates" lazy="true">  
        <key>   <!-- a collection inherits the composite key type -->  
            <column name="foo_string"/>  
            <column name="foo_short"/>  
            <column name="foo_date"/>  
        </key>  
        <element column="foo_date" type="date"/>  
    </set>  
</class> 

 

动态组件 (Dynamic components)

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

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

 

<dynamic-component>映射的语义上来讲,它和 <component>是相同的。 这种映射类型的优点在于通过修改映射文件,就可以具有在部署时检测真实属性的能力.(利用一个DOM解析器, 是有可能在运行时刻操作映射文件的。)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值