Iterate:
String sql = "from Bigque where ID <= 5";
String sql2 = "from Bigque where ID <= 6";
Bigquedao dao = new Bigquedao ();
Session session = Dao.getsession ();
Iterator<bigque> it = session.createquery (sql). Iterate ();
while (It.hasnext ()) {
System.out.println (It.next (). getcontent ());
}
System.out.println ("===========");
Iterator<bigque> it2 = Session.createquery (SQL2). Iterate ();
while (It2.hasnext ()) {
System.out.println (It2.next (). getcontent ());
}
Session.close ();
Results:
Hibernate:select bigque0_.id as col_0_0_ from Cloudroom.bigque bigque0_ where bigque0_.id<=5 hibernate:select bigque0 _.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Select Hibernate:select bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? The blank question Hibernate:select bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Reading questions Hibernate:select bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Judgment Hibernate:select Bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Select =========== hibernate:select bigque0_.id as col_0_0_ from ClouDroom.bigque bigque0_ where bigque0_.id<=6 selection questions the question of reading question question choice Hibernate:select bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Blank questions
Conclusion:
Lazy loading sends an SQL statement first to get all the IDs used and send SQL to the database to find each object
Two times in a session iterate the second iterate will still send a SQL get ID used to send SQL to the database to find each object (if there is this object in the session, it is found in the cache, do not send SQL)
list:
String sql = "from Bigque where ID <= 5"; String sql2 = "from Bigque where ID <= 6";
Bigquedao dao = new Bigquedao ();
Session session = Dao.getsession ();
list<bigque> list = session.createquery (SQL). List ();
for (int i=0; i<list.size (); i++) {
System.out.println (List.get (i). GetContent ());
}
System.out.println ("=============");
List<bigque> list2 = session.createquery (SQL). List ();
for (int i=0; i<list2.size (); i++) {
System.out.println (List2.get (i). GetContent ());
}
Session.close ();
Results:
Hibernate:select bigque0_.id as Id51_, bigque0_. Content as Content51_, bigque0_. Subjectid as subjectid51_ from Cloudroom.bigque bigque0_ where bigque0_.id<=5 choosing the questions of
reading
Questions in the blank question Select
=============
hibernate:select bigque0_.id as id51_, bigque0_. Content as Content51_, bigque0_. Subjectid as subjectid51_ from Cloudroom.bigque bigque0_ where bigque0_.id<=5 choosing the questions of reading questions in the
blank question
Select question
Conclusion: List fetch all the second issue will still go to the database to find