The difference between list and iterator

Source: Internet
Author: User

(1) When querying with the query method, through the HQL statement to get the query object, and the query object operation, the first is to use the list method to obtain the list of the query of the merge output

Public void Listquery () {

Configuration configuration = new configuration (). Configure ();

Sessionfactory factory = Configuration.buildsessionfactory ();

Session session = Factory.opensession ();

Query query = Session.createquery ("from Customers");

list<customers> list = Query.list ();

for (Customers entity:list) {

System. out. println (Entity.tostring ());

}

}

The result of the output is:

The Execute SQL statement for the list is:

Hibernate:select customers0_.id as id0_, customers0_.realname as realname0_, Customers0_.pass as pass0_, Customers0_.sex As sex0_, customers0_.petname as petname0_, Customers0_.email as email0_, customers0_.rdate as rdate0_ from customers cus tomers0_

[Email protected]

(2) using query to get the object of query and output with iterator iterator

Public void Iteratequery () {

Configuration configuration = new configuration (). Configure ();

Sessionfactory factory = Configuration.buildsessionfactory ();

Session session = Factory.opensession ();

Query query = Session.createquery ("from Customers");

Iterator<customers> it = query.iterate ();

Iterate through all the query results

while (It.hasnext ()) {

Customers customer = It.next ();

System. out. println (Customer.tostring ());

}

}

Results of iterator execution:

Hibernate:select Customers0_.id as col_0_0_ from customers customers0_

Hibernate:select customers0_.id as id0_0_, customers0_.realname as realname0_0_, Customers0_.pass as pass0_0_, customers 0_.sex as sex0_0_, customers0_.petname as petname0_0_, Customers0_.email as email0_0_, customers0_.rdate as rdate0_0_ from Customers customers0_ where customers0_.id=?

[Email protected]

Conclusion:

(1) From the above implementation results can be seen to obtain different ways

List is obtained by:list<customers> list = Query.list ();

Iterator Acquisition method:iterator<customers> it = Query.iterate ();

(2) from the execution results can be seen in the list output a statement, and iterator output is two SQL statements, we can think about, why output such an effect?

Because they get the data in a different way, the list () will directly query the database, iterator () will first go to the database to remove the ID, and then really want to traverse an object first to the cache to find, if not found, with the ID condition and then send a SQL to the database, So if there is no data in the cache, the number of times to query the database is n+1

(3) The list only queries the first level cache, and the iterator is checked from the level two cache.

(4) The object returned by the list method is an entity object, and iterator returns a proxy object

(5) The second time the list is issued in the session, the database will still be consulted

(6) Iterate the second time, first find session level cache

The difference between list and iterator

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.