代码如下:
<span style="font-family:KaiTi_GB2312;font-size:18px;"> Student student = (Student)session.get(Student.class, 1); System.out.println(student.getName()); System.out.println(student.getClasses().getName());</span>配置如下:
执行结果:2条语句
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
学生1
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from classes_join classes0_ where classes0_.id=?
高一(1)班
2、连接抓取(Join fetching)
Hibernate通过 在SELECT语句使用OUTER JOIN(外连接)来 获得对象的关联实例或者关联集合。
配置如下:
<many-to-one name="classes" column="classesid" fetch="join"/>
执行结果:
fetch="join",hibernate会通过select语句使用外连接来加载其关联实体或集合,
此时lazy会失效,
一条join语句:
Hibernate: select student0_.id as id1_1_, student0_.name as name1_1_, student0_.class_id as class3_1_1_, classes1_.id as id0_0_, classes1_.name as name0_0_ from student_join student0_ left outer join classes_join classes1_ on student0_.class_id=classes1_.id
where student0_.id=?
学生1
高一(1)班
如果在配置中在添加一个非空属性,连接就成内连接了:
3、子查询抓取(Subselect fetching)
另外发送一条SELECT 语句抓取在前面查询到(或者抓取到)的所有实体对象的关联集合。除非你显式的指定lazy="false" 禁止延迟抓取(lazy fetching),否则只有当你真正访问关联关系的时候,才会执行第二条select语句。
配置文件:
<set name="students" inverse="true" fetch="subselect">
fetch="subselect",另外发送一条select语句抓取在前面查询到的所有实体对象的关联集合。
访问代码如下:
<span style="font-family:KaiTi_GB2312;font-size:18px;"> List classList = session.createQuery("from Classes where id in (1,2,3)").list(); for(Iterator iter = classList.iterator(); iter.hasNext();){ Classes c = (Classes)iter.next(); System.out.println("Class.name=" + c.getName()); Set stuSet = c.getStudents(); System.out.println(stuSet.size()); if(stuSet != null && !stuSet.isEmpty()){ for(Iterator it = stuSet.iterator(); it.hasNext();){ Student s = (Student) it.next(); System.out.println("student.name=" + s.getName()); } }</span> }
执行结果:
当不设fetch="subselect" ,即:<set name="students" inverse="true">,结果如下:
执行了3条查询语句
Hibernate: select classes0_.id as id0_, classes0_.name as name0_ from classes_join classes0_ where classes0_.id in (1 , 2 , 3)
Class.name=高一(1)班
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student_join students0_ where students0_.class_id=?
3
student.name=学生4
student.name=学生6
student.name=学生2
Class.name=高一(2)班
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student_join students0_ where students0_.class_id=?
4
student.name=学生3
student.name=学生4
student.name=学生1
student.name=学生2
Class.name=高一(3)班
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student_join students0_ where students0_.class_id=?
0
当设fetch="subselect" ,即:<set name="students" inverse="true" fetch="subselect">,结果如下:
执行了1条查询语句(嵌套子查询)
Hibernate: select classes0_.id as id0_, classes0_.name as name0_ from classes_join classes0_ where classes0_.id in (1 , 2 , 3)
Class.name=高一(1)班
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student_join students0_ where students0_.class_id in (select classes0_.id from classes_join
classes0_ where classes0_.id in (1 , 2 , 3))
4、批量抓取(Batch fetching)
对查询抓取的优化方案, 通过指定一个主键或外键列表,Hibernate使用单条SELECT语句获取一批对象实例或集合。如果我们不使用hibernate的抓取策略,那么代码如下:
但是可以发现这里有一个问题就是“N+1”问题。我们会发出N多条sql语句,怎么解决问题呢?
在 class 上使用batch-size
batch-size属性,可以批量加载实体类。<class name="Classes" table="t_classes" batch-size="3">
当查classes对象时发出9条hql语句配置过后batch-size=3后会之发9/3=3条hql语句,提高性能。
具体效果如下:
在集合上使用
以上是在class上使用,batch-size还可以在集合上使用:batch-size属性,可以批量加载实体类,<set name="students" inverse="true" cascade="all" batch-size="5">
当查students对象时发出10条hql语句配置过后batch-size=5后会之发10/5=2条hql语句,提高性能
四种抓取策略说明完了, 咱们来宏观一下, 通过例子可以看出, 这四种抓取策略并不是所有的情况都合适的, 例如, 如果我需要初始化的是一个单独的实体, 那么 subselect 对其就没有效果,因为其本身就只需要查询一个对象, 所以 我们可以对hibernate分成如下两类:单端代理抓取和集合抓取。
单端代理抓取可以使用:Join fetching , Select fetching 与 Batch-size ,可以为单个实体的抓取进 行性能优化;
集合抓取: Join fetching , Select fetching ,Subselect fetching , Batch fetching,可以为集合的抓取进行性能优化;
原文:http://blog.csdn.net/wangyongxia921/article/details/43973287