hibernate多表关联配置

关联关系映射通常情况是最难配置正确的.我们从单向关系映射开始,然后考虑
双向关系映射,逐步深入。
单向关联(参考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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate 多对多关联是指两个实体之间存在多对多的关系。在 Hibernate 中,可以通过使用`@ManyToMany`注解来实现多对多关联。 首先,需要定义两个实体类,假设为`EntityA`和`EntityB`,它们之间存在多对多的关系。 ```java @Entity public class EntityA { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // other fields and mappings @ManyToMany @JoinTable( name = "a_b", joinColumns = @JoinColumn(name = "a_id"), inverseJoinColumns = @JoinColumn(name = "b_id") ) private Set<EntityB> entityBs = new HashSet<>(); // getters and setters } @Entity public class EntityB { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // other fields and mappings @ManyToMany(mappedBy = "entityBs") private Set<EntityA> entityAs = new HashSet<>(); // getters and setters } ``` 在上述代码中,`EntityA`类中使用了`@ManyToMany`注解来示与`EntityB`的多对多关联。通过`@JoinTable`注解来指定关联的名称和两个实体之间的外键列。`joinColumns`指定了对应`EntityA`的外键列名,`inverseJoinColumns`指定了对应`EntityB`的外键列名。 在`EntityB`类中,使用了`@ManyToMany(mappedBy = "entityBs")`注解来指定与`EntityA`的关联关系。通过`mappedBy`属性指定了`EntityA`中关联字段的名称。 这样,`EntityA`和`EntityB`之间就建立了多对多的关联。可以通过在代码中操作`entityAs`和`entityBs`集合来实现多对多关联的操作。 同时,还需要在 Hibernate配置文件中配置相关的映射信息,以及其他必要的配置。 以上就是 Hibernate 中实现多对多关联的基本步骤,希望能对你有所帮助!如有更多问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值