Hibernate crawl strategy Fetch specific explanation
One, Hibernate crawl strategy (single-ended agent batch crawl Fetch=select (default)/join)
Test Example:
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
Hibernate crawl strategy Fetch specific explanation
Run 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
Hibernate crawl strategy Fetch specific explanation
At this point, lazy will expire
Run 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 Example:
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) Hibernate crawl strategy fetch specific explanation
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 results: 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 Example:
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 for example the following:
3 Query Statements were run
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", that is: <set name= "Students" inverse= "true" fetch= "subselect", the result is as follows:
1 query Statements were run (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 f Rom Classes_join classes0_ where classes0_.id in (1, 2, 3))
------------------------------------------------------
Summarize:
Hibernate crawl strategy (bulk fetch of single-ended agents) 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
2.hibernate Crawl strategy (bulk fetch of single-ended agents) 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 3.hibernate Crawl strategy (bulk fetch of collection agent) Keep the default, with Fetch= "select", such as: <set name= "Students" inverse= "true" cascade= "All" fetch= "select" > Fetch= "Select" and send a SELECT statement to fetch the current object associated entity or collection
4.hibernate Crawl strategy (bulk fetch of collection agent) Set fetch= "Join", such as: <set name= "Students" inverse= "true" cascade= "All" 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
5.hibernate Crawl strategy (bulk fetch of collection agent) Set fetch= "Subselect", such as: <set name= "Students" inverse= "true" cascade= "All" fetch= "Subselect" > Fetch= "Subselect" and another SELECT statement to fetch the associated collection of all the entity objects previously queried 6.hibernate crawl strategy, Batch-szie application in <class> Batch-size property, able to bulk load entity classes, see: Classes.hbm.xml <class name= "Classes" table= "t_classes" batch-size= "3" >
When checking classes object issued 9 HQL statement configuration after batch-size=3 will send 9/3=3 HQL statement, improve performance 7.hibernate crawl strategy, Batch-szie application on the collection Batch-size property, able to bulk load entity classes, see: Classes.hbm.xml <set name= "Students" inverse= "true" cascade= "All" batch-size= "5" >
When checking students object issued 10 HQL statement configuration after batch-size=5 will send 10/5=2 HQL statement, improve performance Overall Analysis: The default is Fetch= "select" When configuring Fetch= "join" when directly querying the included objects or the collection lazy invalidation. |
The fetch of Hibernate