首页 新闻 搜索 专区 学院

Hibernate中many to one 生成的sql语句

0
悬赏园豆:10 [待解决问题]

当用<mang to one >元素和<one to one>元素组合实现双向一对一的关联时为什么生成的是外联结语句,而<mang to one >元素和Set元素的<one to many>组合实现双向一对多的关联却没有生成表连接语句。

双向一对一:

@Test
publicvoid testGet(){
//1. 默认情况下对关联属性使用懒加载
Department dept =(Department) session.get(Department.class,1);
System.out.println(dept.getDeptName());
 
//2. 所以会出现懒加载异常的问题.
session.close();
Manager mgr = dept.getMgr();
System.out.println(mgr.getClass());
System.out.println(mgr.getMgrName());
 
//3. 查询 Manager 对象的连接条件应该是 dept.manager_id = mgr.manager_id
//而不应该是 dept.dept_id = mgr.manager_id
mgr = dept.getMgr();
System.out.println(mgr.getMgrName());
 
 
}
通过dept查询关联的mgr:
第一步:查询出mgr
select
manager0_.MGR_ID as MGR_ID1_2_1_,
manager0_.MGR_NAME as MGR_NAME2_2_1_,
department1_.DEPT_ID as DEPT_ID1_1_0_,
department1_.DEPT_NAME as DEPT_NAM2_1_0_,
department1_.MGR_ID as MGR_ID3_1_0_
from
MANAGERS manager0_
left outer join
DEPARTMENTS department1_
on manager0_.MGR_ID=department1_.MGR_ID
where
manager0_.MGR_ID=?
第二步:查询出mgr关联的dept
select
department0_.DEPT_ID as DEPT_ID1_1_0_,
department0_.DEPT_NAME as DEPT_NAM2_1_0_,
department0_.MGR_ID as MGR_ID3_1_0_
from
DEPARTMENTS department0_
where
department0_.MGR_ID=?

双向一对n,没有表连接语句

TomStudio的主页 TomStudio | 初学一级 | 园豆:58
提问于:2016-12-08 21:34
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册