The difference between list () and iterate () in Hibernate

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.