1.class上的延迟加载
我们通过get和load来查询,get默认不使用延迟加载,load使用延迟加载,延迟加载必须在同一个Session范围内
Get方法:
@Test
public void testQueryWGet(){
Session session = HibernateUtils.getSession();
try {
//get方法不使用延迟加载,发出sql的
User user = (User) session.get(User.class, "1");
System.out.println(user.getUname());
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateUtils.closeResource(session);
}
}
Load方法:
@Test
public void testQueryWLoad(){
Session session = HibernateUtils.getSession();
try {
//load方法默认使用延迟加载,不发出sql的
User user = (User) session.load(User.class, "1");
//使用到对象的成员时来发出sql
System.out.println(user.getUname());
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateUtils.closeResource(session);
}
}
延迟加载必须在同一个Session内
/**
* 类上的延迟加载要在同一个Session之内
*/
@Test
public void testQueryWLoad1(){
Session session = HibernateUtils.getSession();
User user = null;
try {
//load方法默认使用延迟加载,不发出sql的
user = (User) session.load(User.class, "1");
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateUtils.closeResource(session);
}
//Session关闭了就不会有延迟加载,不发sql
System.out.println(user.getUname());
}
lazy:类上延迟加载的开关。当前类是否使用延迟加载,默认是true使用,false不使用
<!--
lazy:当前类是否使用延迟加载,默认是true使用,false不使用
-->
<class name="User" table="t_user" lazy="false">
<!-- id
是主键映射配置
-->
<id name="userId" column="user_id">
2.集合的延迟加载
集合的延迟加载是在一对多和多对多
默认情况集合是有延迟加载的
@Test
public void testQuery(){
Session session = HibernateUtils.getSession();
try {
Team team = (Team) session.load(Team.class, 1);
//发出查询team的sql
System.out.println(team);
Set<Emp> emps = team.getSet();
//发出根据t_id查询员工的sql
System.out.println(emps);
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateUtils.closeResource(session);
}
}
输出
Hibernate: select team0_.t_id as t_id1_1_0_, team0_.t_name as t_name2_1_0_, team0_.loc as loc3_1_0_ from t_team team0_ where team0_.t_id=? Team [tId=1, tName=公牛队, loc=芝加哥] Hibernate: select set0_.t_id as t_id6_1_0_, set0_.emp_no as emp_no1_0_0_, set0_.emp_no as emp_no1_0_1_, set0_.ename as ename2_0_1_, set0_.birthday as birthday3_0_1_, set0_.gender as gender4_0_1_, set0_.address as address5_0_1_, set0_.t_id as t_id6_0_1_ from t_emp set0_ where set0_.t_id=? [Emp [empNo=1, ename=乔丹, birthday=2015-09-16 15:01:36.0, gender=1, address=芝加哥], Emp [empNo=2, ename=菲尔, birthday=2015-09-16 15:01:36.0, gender=1, address=芝加哥]]
|
如果set上的lazy=false,没有延迟加载效果。
输出:
Hibernate: select team0_.t_id as t_id1_1_0_, team0_.t_name as t_name2_1_0_, team0_.loc as loc3_1_0_ from t_team team0_ where team0_.t_id=? Hibernate: select set0_.t_id as t_id6_1_0_, set0_.emp_no as emp_no1_0_0_, set0_.emp_no as emp_no1_0_1_, set0_.ename as ename2_0_1_, set0_.birthday as birthday3_0_1_, set0_.gender as gender4_0_1_, set0_.address as address5_0_1_, set0_.t_id as t_id6_0_1_ from t_emp set0_ where set0_.t_id=? Team [tId=1, tName=公牛队, loc=芝加哥] [Emp [empNo=2, ename=菲尔, birthday=2015-09-16 15:01:36.0, gender=1, address=芝加哥], Emp [empNo=1, ename=乔丹, birthday=2015-09-16 15:01:36.0, gender=1, address=芝加哥]]
|
当lazy=true的时候,如果查询集合的数量时,效率低下。我们需要把lazy=extra,效率比较高
/**
* lazy=extra
*/
@Test
public void testQuery2(){
Session session = HibernateUtils.getSession();
try {
Team team = (Team) session.load(Team.class, 1);
//发出查询team的sql
System.out.println(team);
Set<Emp> emps = team.getSet();
//发出一条智能高效的sql
System.out.println(emps.size());
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateUtils.closeResource(session);
}
}
输出:
Hibernate: select team0_.t_id as t_id1_1_0_, team0_.t_name as t_name2_1_0_, team0_.loc as loc3_1_0_ from t_team team0_ where team0_.t_id=? Team [tId=1, tName=公牛队, loc=芝加哥] Hibernate: select count(emp_no) from t_emp where t_id =? 2
|
注意:集合的延迟加载效果不受类上的延迟加载影响。
3.单端延迟加载
针对多对一和一对一的延迟加载
默认情况lazy=proxy使用延迟加载
/**
* 默认情况many-to-one=proxy
*/
@Test
public void testQuery(){
Session session = HibernateUtils.getSession();
try {
//不发sql
Emp emp = (Emp) session.load(Emp.class, 1);
//发出一条根据emp_no查询员工的sql
System.out.println(emp);
//不发
Team team = emp.getTeam();
//发出一条根据t_id查询球队的sql
System.out.println(team);
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateUtils.closeResource(session);
}
}
输出:
Hibernate: select emp0_.emp_no as emp_no1_0_0_, emp0_.ename as ename2_0_0_, emp0_.birthday as birthday3_0_0_, emp0_.gender as gender4_0_0_, emp0_.address as address5_0_0_, emp0_.t_id as t_id6_0_0_ from t_emp emp0_ where emp0_.emp_no=? Emp [empNo=1, ename=乔丹, birthday=2015-09-16 16:05:14.0, gender=1, address=芝加哥] Hibernate: select team0_.t_id as t_id1_1_0_, team0_.t_name as t_name2_1_0_, team0_.loc as loc3_1_0_ from t_team team0_ where team0_.t_id=? Team [tId=1, tName=公牛队, loc=芝加哥] |
Lazy=false
/**
* lazy=false
*/
@Test
public void testQuery1(){
Session session = HibernateUtils.getSession();
try {
//不发sql
Emp emp = (Emp) session.load(Emp.class, 1);
//发出一条根据emp_no查询员工的sql,发出一条根据t_id查询球队的sql
System.out.println(emp);
//不发
Team team = emp.getTeam();
//不发
System.out.println(team);
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateUtils.closeResource(session);
}
}
输出:
Hibernate: select emp0_.emp_no as emp_no1_0_0_, emp0_.ename as ename2_0_0_, emp0_.birthday as birthday3_0_0_, emp0_.gender as gender4_0_0_, emp0_.address as address5_0_0_, emp0_.t_id as t_id6_0_0_ from t_emp emp0_ where emp0_.emp_no=? Hibernate: select team0_.t_id as t_id1_1_0_, team0_.t_name as t_name2_1_0_, team0_.loc as loc3_1_0_ from t_team team0_ where team0_.t_id=? Emp [empNo=1, ename=乔丹, birthday=2015-09-16 16:05:14.0, gender=1, address=芝加哥] Team [tId=1, tName=公牛队, loc=芝加哥] |
如果Team类的延迟加载被禁用,many-to-one lazy=proxy
/**
* 如果Team类的延迟加载被禁用,many-to-one lazy=proxy
*/
@Test
public void testQuery2(){
Session session = HibernateUtils.getSession();
try {
//不发sql
Emp emp = (Emp) session.load(Emp.class, 1);
//发出一条根据emp_no查询员工和球队的连接查询的sql
System.out.println(emp);
//不发
Team team = emp.getTeam();
//不发
System.out.println(team.gettName());
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateUtils.closeResource(session);
}
}
输出
Hibernate: select emp0_.emp_no as emp_no1_0_0_, emp0_.ename as ename2_0_0_, emp0_.birthday as birthday3_0_0_, emp0_.gender as gender4_0_0_, emp0_.address as address5_0_0_, emp0_.t_id as t_id6_0_0_, team1_.t_id as t_id1_1_1_, team1_.t_name as t_name2_1_1_, team1_.loc as loc3_1_1_ from t_emp emp0_ left outer join t_team team1_ on emp0_.t_id=team1_.t_id where emp0_.emp_no=? Emp [empNo=1, ename=乔丹, birthday=2015-09-16 16:05:14.0, gender=1, address=芝加哥] 公牛队 |