Differences between List and iteraor and Listiteraor

Source: Internet
Author: User

Differences between List and iteraor and Listiteraor

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 void listQuery() {      Configuration configuration = new Configuration().configure();      SessionFactory factory = configuration.buildSessionFactory();      Session session = factory.openSession();        Query query = session.createQuery("fromCustomers");      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 aspass0 _, customers0 _. sex as sex0 _, customers0 _. petName as petName0 _, customers0 _. email as email0 _, customers0 _. rdate as rdate0 _ from customerscustomers0 _

Cn. csdn. products. domain. MERs @ 5bf624

(2) obtain the Query object through the Query and output it with the 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 (); // traverse all the 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 _. petNameas petName0_0 _, customers0 _. email as email0_0 _, customers0 _. rdate as rdate0_0_from MERs customers0 _ where customers0 _. id =?

Cn. csdn. products. domain. MERs @ 1d13272

Analysis: The statement shows that

  • When the list () method is executed, it directly runs the query statement required for the query results.
  • The iterator () method first executes the query to obtain the Object ID, and then retrieves the object to be queried based on each ID value.

Summary:

Through the analysis of the above two methods, we found that the two Query methods, list () and iterate (), both of which are listing the result set. They have three differences,

  • Different types are returned. list () returns List, iterate () returns Iterator,
  • The methods for getting data are different. list () will directly query the database, and iterate () will first retrieve the IDs in the database, and then search for the IDs in the cache when traversing an object, if no data is found, an SQL statement is sent to the database based on the id. If no data is found in the cache, the number of queries to the database is n + 1.
  • Iterate queries Level 2 caches, and list queries Level 1 caches only.
  • Each object in the list returned by List () is the original object, and the object returned by iterate () is the proxy object. (this can be found by debug)

In general, latency loading and direct query of data sources are involved. The language does not have any boundaries, just like. NET Ienumerator and IqueryInterface.

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.