hibernate抓取策略
一、hibernate抓取策略(单端代理的批量抓取fetch=select(默认)/join)
测试用例:
Student student = (Student)session.get(Student.class, 1);
System.out.println(student.getName());
System.out.println(student.getClasses().getName());1)保持默认,同fetch="select",如:<many-to-one name="classes" column="classesid" fetch="select"/>
fetch="select",另外发送一条select语句抓取当前对象关联实体或集合
执行结果: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:selectclasses0_.idasid0_0_,classes0_.nameasname0_0_fromclasses_joinclasses0_whereclasses0_.id=?
高一(1)班======================================
2)设置fetch="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)班======================================================
二、hibernate抓取策略(集合代理的批量抓取,fetch=select(默认)/join/subselect)
测试用例:
Classes c = (Classes) session.load(Classes.class, new Integer(1));
System.out.println("Class.name="+c.getName());
SetstuSet=c.getStudents();
System.out.println(stuSet.size());
if(stuSet!=null&&!stuSet.isEmpty()){
for(Iteratorit=stuSet.iterator();it.hasNext();){
Students=(Student)it.next();
System.out.println("student.name="+s.getName());
}
}1)保持默认,同fetch="select",如:<set name="students" inverse="true" fetch="select">
fetch="select",另外发送一条select语句抓取当前对象关联实体或集合
测试结果:2条独立的查询语句
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from classes_join classes0_ where classes0_.id=?
Class.name=高一(1)班
Hibernate:selectstudents0_.class_idasclass3_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.nameasname1_0_,students0_.class_idasclass3_1_0_fromstudent_joinstudents0_wherestudents0_.class_id=?
9
student.name=学生7
student.name=学生3
student.name=学生1
student.name=学生8
student.name=学生2
student.name=学生4
student.name=学生5
student.name=学生9
student.name=学生6(2)设置fetch="join",如:<set name="students" inverse="true" fetch="join">
fetch="join",hibernate会通过select语句使用外连接来加载其关联实体或集合
此时lazy会失效
测试结果:1条独立的join查询语句
Hibernate: select classes0_.id as id0_1_, classes0_.name as name0_1_, students1_.class_id as class3_3_, students1_.id as id3_, students1_.id as id1_0_, students1_.name as name1_0_, students1_.class_id as class3_1_0_ from classes_join classes0_ left outer join student_join students1_ on classes0_.id=students1_.class_id where classes0_.id=?
Class.name=高一(1)班
9
student.name=学生6
student.name=学生4
student.name=学生9
student.name=学生7
student.name=学生2
student.name=学生3
student.name=学生8
student.name=学生1
student.name=学生5(3)设置fetch="subselect",如:用在查询语句中<set name="students" inverse="true" fetch="subselect">
fetch="subselect",另外发送一条select语句抓取在前面查询到的所有实体对象的关联集合
测试用例:
List classList = session.createQuery("from Classes where id in (1,2,3)").list();
for(Iteratoriter=classList.iterator();iter.hasNext();){
Classesc=(Classes)iter.next();
System.out.println("Class.name="+c.getName());
SetstuSet=c.getStudents();
System.out.println(stuSet.size());
if(stuSet!=null&&!stuSet.isEmpty()){
for(Iteratorit=stuSet.iterator();it.hasNext();){
Students=(Student)it.next();
System.out.println("student.name="+s.getName());
}
}
}当不设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:selectstudents0_.class_idasclass3_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.nameasname1_0_,students0_.class_idasclass3_1_0_fromstudent_joinstudents0_wherestudents0_.class_id=?
9
student.name=学生8
student.name=学生5
student.name=学生3
student.name=学生9
student.name=学生7
student.name=学生1
student.name=学生4
student.name=学生6
student.name=学生2
Class.name=高一(2)班
Hibernate:selectstudents0_.class_idasclass3_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.nameasname1_0_,students0_.class_idasclass3_1_0_fromstudent_joinstudents0_wherestudents0_.class_id=?
4
student.name=学生3
student.name=学生4
student.name=学生1
student.name=学生2
Class.name=高一(3)班
Hibernate:selectstudents0_.class_idasclass3_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.nameasname1_0_,students0_.class_idasclass3_1_0_fromstudent_joinstudents0_wherestudents0_.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:selectstudents0_.class_idasclass3_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.nameasname1_0_,students0_.class_idasclass3_1_0_fromstudent_joinstudents0_wherestudents0_.class_idin(selectclasses0_.idfromclasses_joinclasses0_whereclasses0_.idin(1,2,3))
9
student.name=学生8
student.name=学生4
student.name=学生5
student.name=学生9
student.name=学生6
student.name=学生2
student.name=学生3
student.name=学生1
student.name=学生7
Class.name=高一(2)班
4
student.name=学生3
student.name=学生4
student.name=学生2
student.name=学生1
Class.name=高一(3)班