Transferred from: http://windchenqing.blog.163.com/blog/static/105926653201085114655128/
One, Hibernate crawl strategy (single-ended agent batch crawl Fetch=select (default)/join)
Test Case:
Student Student = (Student) session.get (Student.class, 1);
System.out.println (Student.getname ());
System.out.println (Student.getclasses (). GetName ());
1) Keep the default, with Fetch= "select", such as:
<many-to-one name= "Classes" column= "Classesid" fetch= "select"/>
Fetch= "Select" and send a SELECT statement to fetch the current object associated entity or collection
Execution Result: 2 statements
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=?
Student 1
Hibernate:select classes0_.id as id0_0_, classes0_.name as name0_0_ from Classes_join classes0_ where classes0_.id=?
Class (1)
======================================
2) Set fetch= "Join", such as:
<many-to-one name= "Classes" column= "Classesid" fetch= "join"/>
Fetch= "Join", Hibernate uses an outer join to load its associated entity or collection through the SELECT statement
At this point, lazy will expire
execution Result: A join statement
Hibernate:select student0_.id as id1_1_, student0_.name as name1_1_, student0_.class_id as class3_1_1_, classes1_.id as I d0_0_, Classes1_.name as name0_0_ from Student_join student0_ left outer joins Classes_join Classes1_ on student0_.class_id =classes1_.id where student0_.id=?
Student 1
Class (1)
======================================================
Two, Hibernate crawl strategy (collection Agent batch crawl, Fetch=select (default)/join/subselect)
Test Case:
Classes C = (Classes) session.load (Classes.class, New Integer (1));
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 ());
}
}
1) Keep the default, with Fetch= "select", such as:
<set name= "Students" inverse= "true" fetch= "select" >
Fetch= "Select" and send a SELECT statement to fetch the current object associated entity or collection
Test Results: 2 Independent query Statements
Hibernate:select classes0_.id as id0_0_, classes0_.name as name0_0_ from Classes_join classes0_ where classes0_.id=?
Class.name= (1) class
Hibernate:select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as Nam e1_0_, students0_.class_id as class3_1_0_ from Student_join students0_ where students0_.class_id=?
9
Student.name= Students 7
Student.name= Students 3
Student.name= Students 1
Student.name= Students 8
Student.name= Students 2
Student.name= Students 4
Student.name= Students 5
Student.name= Students 9
Student.name= Students 6
(2) Set fetch= "Join", such as:
<set name= "Students" inverse= "true" fetch= "Join" >
Fetch= "Join", Hibernate uses an outer join to load its associated entity or collection through the SELECT statement
At this point, lazy will expire
test Result: 1 Independent join query Statements
Hibernate:select classes0_.id as id0_1_, classes0_.name as name0_1_, students1_.class_id as class3_3_, students1_.id as I D3_, students1_.id as id1_0_, students1_.name as name1_0_, students1_.class_id as class3_1_0_ from Classes_join classes0_ Left outer joins Student_join students1_ on classes0_.id=students1_.class_id where classes0_.id=?
Class.name= (1) class
9
Student.name= Students 6
Student.name= Students 4
Student.name= Students 9
Student.name= Students 7
Student.name= Students 2
Student.name= Students 3
Student.name= Students 8
Student.name= Students 1
Student.name= Students 5
(3) Set fetch= "Subselect", such as: used in query statements
<set name= "Students" inverse= "true" fetch= "Subselect" >
Fetch= "Subselect" and another SELECT statement to fetch the associated collection of all the entity objects previously queried
test Case:
List classlist = Session.createquery ("From Classes where ID in ()"). 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 ());
}
}
}
when not set fetch= "Subselect", namely: <set name= "Students" inverse= "true"; The result is as follows:
3 Query statements executed
Hibernate:select classes0_.id as id0_, classes0_.name as name0_ from Classes_join classes0_ where classes0_.id in (1, 2 , 3)
Class.name= (1) class
Hibernate:select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as Nam e1_0_, students0_.class_id as class3_1_0_ from Student_join students0_ where students0_.class_id=?
9
Student.name= Students 8
Student.name= Students 5
Student.name= Students 3
Student.name= Students 9
Student.name= Students 7
Student.name= Students 1
Student.name= Students 4
Student.name= Students 6
Student.name= Students 2
Class.name= (2) class
Hibernate:select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as Nam e1_0_, students0_.class_id as class3_1_0_ from Student_join students0_ where students0_.class_id=?
4
Student.name= Students 3
Student.name= Students 4
Student.name= Students 1
Student.name= Students 2
Class.name= (3) class
Hibernate:select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as Nam e1_0_, students0_.class_id as class3_1_0_ from Student_join students0_ where students0_.class_id=?
0
when not set fetch= "Subselect", namely: <set name= "Students" inverse= "true" fetch= "subselect", the result is as follows:
1 Query statements executed (nested subqueries)
Hibernate:select classes0_.id as id0_, classes0_.name as name0_ from Classes_join classes0_ where classes0_.id in (1, 2 , 3)
Class.name= (1) class
Hibernate:select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as Nam e1_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))
9
Student.name= Students 8
Student.name= Students 4
Student.name= Students 5
Student.name= Students 9
Student.name= Students 6
Student.name= Students 2
Student.name= Students 3
Student.name= Students 1
Student.name= Students 7
Class.name= (2) class
4
Student.name= Students 3
Student.name= Students 4
Student.name= Students 2
Student.name= Students 1
Class.name= (3) class
0