hibernate多表关联配置

来源:互联网 发布:mac口红brave red试色 编辑:程序博客网 时间:2024/05/29 11:47

关联关系映射通常情况是最难配置正确的.我们从单向关系映射开始,然后考虑
双向关系映射,逐步深入。
单向关联(参考Hibernate Reference Documentation)
一、多对一(many-to-one)
单向 many-to-one 关联是最常见的单向关联关系。
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class
>
create table Person ( personId bigint not null primary key, addressId bigint not null )
create table Address ( addressId bigint not null primary key )

二、一对一(One-to-one)
基于外键关联的单向一对一关联和单向多对一关联几乎是一样的。唯一的不同就是单向一对一关
联中的外键字段具有唯一性约束。
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class
>
create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )
基于主键关联的单向一对一关联通常使用一个特定的 id 生成器,然而在这个例子中我们掉换了
关联的方向:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
</class>
<class name="Address">
<id name="id" column="personId">
<generator class="foreign">
<param name="property"
>person</param>
</generator>
</id>
<one-to-one name="person" constrained="true"/>
</class
>
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )

一对多(one-to-many)
基于外键关联的单向一对多关联是一种很少见的情况,我们不推荐使用它。
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses">
<key column="personId"
not-null="true"/>
<one-to-many class="Address"/>
</set>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class
>
create table Person ( personId bigint not null primary key )
create table Address ( addressId bigint not null primary key, personId bigint not null )
我们认为对于这种关联关系最好使用连接表。
连接表实际上就是把两表的关系放在另外一张表上,起连接作用,双向关联中经常用到。

双向关联
一、一对多(one to many)/多对一(many to one)
双向多对一关联 是最常见的关联关系。
首先建立两张表,部门表与角色表,实践中我们知道部门表与角色表的关系的一对多的,即部门表 1:n角色表,建表如下:
create table roleinfo(
rid number(10) not null primary key,
rolename varchar2(50) not null,
//用于关联部门表
deptid number(10)
)
create table department(
did number(10) not null primary key,
departname varchar2(50) not null
)
Role.hbm.xml映射文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="myclass.entity">
    <class name="RolePojo" table="roleinfo">
    <id name="rid" column="rid" type="integer">
    <generator class="sequence">
    <param name="sequence">seq_student</param>
    </generator>
    </id>
    <property name="rolename" column="rolename"></property>
    <!-- 配置角色与部门多对一关联 -->
    <many-to-one name="departpojo" class="DepartPojo" cascade="all" column="deptid"></many-to-one>
    </class>
    </hibernate-mapping>
Depart.hbm.xml映射文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="myclass.entity">
    <class name="DepartPojo" table="department">
    <id name="did" column="did" type="integer">
    <generator class="sequence">
    <param name="sequence">PIC_SEQUENCE1</param>
    </generator>
    </id>
    <property name="departname" column="departname"></property>
    <set name="rolepojo" cascade="all" inverse="true">
    <key>
    <column name="deptid"></column>
    </key>
     <!-- 配置部门与角色一对多关联 -->
    <one-to-many class="RolePojo"></one-to-many> 
    </set>cascade="all"></one-to-one>
    </class>
    </hibernate-mapping>
这样一个角色与部门的多对一关系就建好了,写两个表的POJO类,再写个Test就可以观察到效果了。

二、一对一(One-to-one)
基于外键关联的双向一对一关联也很常见。
首先建立两张表,关系为个人表 1:1地址表
create table Person ( 
personId bigint not null primary key, 
addressId bigint not null unique )
create table Address ( 
addressId bigint not null primary key )
基于外键关联的一对一关联映射文件配置如下:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class
>
也可以使用基于主键关联的一对一关联,就节省了一个外键字段了,但需要使用特定的 id 生成器。
首先建立两张表,如下:
create table Person ( 
personId bigint not null primary key 
)
create table Address ( 
personId bigint not null primary key
 )
映射文件配置如下:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address"/>
</class>

<class name="Address">
<id name="id" column="personId">
<generator class="foreign">
<param name="property"
>person</param>
</generator>
</id>
<one-to-one name="person"
constrained="true"/>
</class
>
特说明一下,还有基于连接表的双向一对一关联也是可行的,但极为罕见。这里不作介绍。
三、多对多(many-to-many)
这里我们使用连接表来表示双向关联,意思就是另外建立一张表表示两表的关系。建立三张表,实践中我们知道用户表与角色表的关系的多对

多的,即用户表 m:n角色表,建表如下:
create table userinfo(
userid number(10) not null primary key,
username varchar2(50) not null,
)
create table roleinfo(
rid number(10) not null primary key,
rolename varchar2(50) not null
)
//用于关联用户表与角色表,这就是连接表
create table userinfo_role_link(
userid number(10),
rid number(10),
primary key(userid,rid)
)
User.hbm.xml映射文件配置如下:
 <class name="UserPojo" table="userinfo">
    <id name="userid" column="userid" type="integer">
    <generator class="sequence">
    <param name="sequence">seq_stu</param>
    </generator>
    </id>
    <property name="username" column="username"></property>
     <!-- 配置用户表对角色表的关联 -->
    <set name="rolepojo" table="userinfo_role_link" cascade="all">
    <key>
    <column name="userid"></column>
    </key>
    <many-to-many class="RolePojo" column="rid"></many-to-many>
    </set>
    </class>
Role.hbm.xml映射文件配置如下:
 <class name="RolePojo" table="roleinfo">
    <id name="rid" column="rid" type="integer">
    <generator class="sequence">
    <param name="sequence">seq_student</param>
    </generator>
    </id>
    <property name="rolename" column="rolename"></property>
    <!--用于实现用户与角色多对多的关联-->
    <set name="userPojo" table="userinfo_role_link" cascade="all">
    <key>
    <column name="rid"></column>
    </key>
    <many-to-many class="UserPojo" column="userid"></many-to-many>
    </set>
    </class>
这样多对多的关联也建好了。在写此文中参考了Hibernate Reference
Documentation,本文档里面也写的很详细,是个不错的教程。

0 0
原创粉丝点击