Hibernate关联关系

来源:互联网 发布:ubuntu安装wireshark 编辑:程序博客网 时间:2024/06/11 04:37

简介

Hibernate关联关系主要有:一对一,一对多,多对多。

这里主要针对一对多展开讨论。

详述

在一对多的关联关系中,在单一的一方需要定义一个集合存放一对多中的多的一方,但是这里要注意,在单一的一方定义集合产生的属性在对应的数据表中并没有对应的字段,而是单纯的在持久化类中定义的,一对多的关联是通过Hibernate自身来处理的。

在多对多的情况中一般是将一个多对多的关联关系定义一个中间表,分解成两个多对一或者一对多。

注意:在同一个表的映射文件中可以定义多个一对多,多对一等的映射关系,具体只需要在当前的持久化类中添加相应的属性以指示到对应的关联类就行。

以下是一对多的配置方式(在这个配置文件中没有给set定义对应的表的字段,因为在表中压根就没有对应的字段)

<hibernate-mapping>        <class name="com.suxiaolei.hibernate.pojos.Customer" table="customer">            <!-- 主键设置 -->            <id name="id" type="string">                <column name="id"></column>                <generator class="uuid"></generator>            </id>            <!-- 属性设置 -->            <property name="username" column="username" type="string"></property>            <property name="balance" column="balance" type="integer"></property>                        <set name="orders" inverse="true" cascade="all">                <key column="customer_id" ></key>                <one-to-many class="com.suxiaolei.hibernate.pojos.Order"/>            </set>        </class></hibernate-mapping>


0 0
原创粉丝点击