Differences between hibernate-list and iterator Methods

Source: Internet
Author: User

Original: http://blog.csdn.net/hanxiaoshuang321123/article/details/7103414

1. First, let's take a look at two examples to compare them.

(1) When querying using the query method, you can use the hql statement to obtain the query object and perform operations on the query object. First, you can use the list method to obtain the list set of the query and output it.

Public voidListquery (){

Configuration configuration =NewConfiguration (). Configure ();

Sessionfactory factory = configuration. buildsessionfactory ();

Session session = factory. opensession ();

Query query =Session. Createquery ("from MERs ");

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

For(Customers entity: List ){

System.Out. Println (entity. tostring ());

}

}

The output result is:

List execution SQL statement:

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 MERs customers0 _

CN. csdn. Products. domain. MERs @ 5bf624

(2) obtain the query object through the query and output it with the iterator.

Public voidIteratequery (){

Configuration configuration =NewConfiguration (). Configure ();

Sessionfactory factory = configuration. buildsessionfactory ();

Session session = factory. opensession ();

Query query = session. createquery ("from MERs ");

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

// Print all query results

While(It. hasnext ()){

Customers customer = it. Next ();

System.Out. Println (customer. tostring ());

}

}

Iterator execution result:

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 _, customers0 _. sex as sex0_0 _, customers0 _. petname as petname0_0 _, customers0 _. email as email0_0 _, customers0 _. rdate as rdate0_0 _ from MERs customers0 _ Where customers0 _. id =?

CN. csdn. Products. domain. MERs @ 1d13272

 

Conclusion:

(1) from the preceding execution results, we can see that the acquisition methods are different.

List: List <customers> List =Query. List ();

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

(2) From the execution result, we can see that list outputs a statement, while iterator outputs two SQL statements. Let's look at why this effect is output?

Because they obtain different data in different ways, list () queries the database directly, and iterator () retrieves IDs from the database first, then, when you really want to traverse an object, first find it in the cache. If you cannot find it, use the ID as the condition and then send an SQL statement to the database. In this way, if there is no data in the cache, the number of queries to the database is n + 1

(3) List queries only the level-1 cache, while iterator queries the level-2 cache.

(4) Objects returned by the list method are all object objects, while iterator returns proxy objects.

(5) The list in the session will be issued for the second time and will still be queried in the database.

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

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.