Use the list () method to get the query results, each time a query statement, get all the data using the iterate () method to get the results of the query, first issued an SQL statement to query the ID that satisfies the condition data, and then to query the records by these IDs, that is, to execute the n+1-bar SQL statement (N Number of records that match the criteria)
Sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
Session = Sessionfactory.opensession ();
Query query = Session.createquery ("From house as h");
System.out.println ("Use iterate () method to query data");
Iterator
while (It.hasnext ()) {
House House = It.next ();
System.out.println ("title:" +house.gettitle ());
}
System.out.println ("-------------------");
SYSTEM.OUT.PRINTLN ("Use list () method to query data");
list
For (house House:result) {
System.out.println ("title:" +house.gettitle ());
}
Sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
Session = Sessionfactory.opensession ();
Query query = Session.createquery ("From house as h");
SYSTEM.OUT.PRINTLN ("Use list () method to query data");
list
For (house House:result) {
System.out.println ("title:" +house.gettitle ());
}
System.out.println ("-------------------");
System.out.println ("Use iterate () method to query data");
Iterator
while (It.hasnext ()) {
House House = It.next ();
System.out.println ("title:" +house.gettitle ());
}
Sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
Session = Sessionfactory.opensession ();
Query query = Session.createquery ("From house as h");
SYSTEM.OUT.PRINTLN ("Use list () method to query data");
List result1 = Query.list ();
for (int i=0;i<result1.size (); i++) {
House House = (House) result1.get (i);
System.out.println ("title:" +house.gettitle ());
}
System.out.println ("----------------------");
System.out.println ("Execute list () method again");
List result2 = Query.list ();
for (int i=0;i<result2.size (); i++) {
House House = (House) result2.get (i);
System.out.println ("title:" +house.gettitle ());
}
Sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
Sessessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
Session = Sessionfactory.opensession ();
Query query = Session.createquery ("From house as h");
System.out.println ("Use iterate () method to query data");
Iterator it1 = Query.iterate ();
while (It1.hasnext ()) {
House House = (house) it1.next ();
System.out.println ("title:" +house.gettitle ());
}
System.out.println ("----------------------");
System.out.println ("One more execution of the iterate () method");
Iterator it2 = Query.iterate ();
while (It2.hasnext ()) {
House House = (house) it2.next ();
System.out.println ("title:" +house.gettitle ());
}
It is not difficult to draw conclusions from the above examples:
The list () method will not read the data in the cache, it always queries all eligible data directly from the database at once, and writes the acquired data to the cache.
The iterate () method is to obtain the ID of the eligible data, first, according to the ID in the cache to find matching data, if the cache does not meet the criteria of the data, and then query the database.
So when you use it, please use it according to your own circumstances to save resources.
This article link: http://www.snowdi.com/699.html→ reprint please indicate article from Snow Ros net