hibernate关联查询

 所谓关联查询即查询某个表的时候会连带查询出相关联的表的数据。

1) 一对一:例如一个员工表与员工详细表的对应关系

2) 一对多:一张表的一条记录可以对应另外一张表的多条记录。比如部门与员工的关系。

3) 多对一:与一对多相反,例如员工表与部门表之间的关系。通过外键来维护。

4) 多对多:例如一个员工有多个角色,一个角色 可以对应多个员工。多对多通常需要一张中间表来维护关联关系。

一、在创建过程中要用到的表和序列如下:

-- 部门表
create table t_dept(
id number(11) primary key ,
deptcode varchar2(20) unique not null ,
deptname varchar2(50) not null
);
-- 管理员表
create table t_admin(
id number(11) primary key ,
account varchar2(50) unique not null ,
password varchar2(20) not null ,
deptid number(11) references t_dept(id)
);
-- 角色表
create table t_role(
id number(11) primary key ,
rolecode varchar2(50) not null,
rolename varchar2(50) not null ,
status char(1) default '0'
);
-- 用户角色关系表(生成实体类时,此表不需要操作)
create table t_user_role(
roleid number(11) references t_role(id) not null ,
userid number(11) references t_admin(id) not null
);
-- 用户详情表
create table t_user_detail(
id number(11) primary key ,
addr varchar2(200),
education varchar2(20)
);

create sequence seq_t_admin;
create sequence seq_t_role;
create sequence seq_t_dept;
二、一些java文件和相对于的映射文件的配置如下所示:
①一对一关联:

②一对多关联:一对多,不配admin的原因是,我们现在只是单向操作,只通过部门来寻找部门的人员信息,而没有通过人员来查找部门,但是到后面会配;

③多对一:

④多对多:















原文地址:https://www.cnblogs.com/xie-qi/p/12873083.html