The difference between the hibernate-list () and the Iterate () method

Source: Internet
Author: User

For the list method, hibernate actually gets all the records through a select SQL. and read it out and fill it back in the Pojo.
The iterate method is to first get all the IDs of the records that meet the query criteria through a select SQL, and then loop through the collection of IDs, taking the records corresponding to each ID in a separate select SQL, and then filling in the Pojo to return.
That is, a SQL complete is required for the list operation. For iterate operations, n+1 SQL is required.
It seems that the iterate method may seem redundant, but in different cases there is still a unique effect, such as querying large amounts of data, and if the result set is taken out once with the list method, the overhead of memory might be unbearable.
On the other hand, for our current cache mechanism, the list method will not read the data from the cache, it always reads all eligible records directly from the database at once. And the iterate method because each time according to the ID to , such an implementation mechanism to read data from the cache provides the possibility, hibernate first will be based on this ID in the local cache to find the corresponding data, if not found, then go to the database retrieval.

The two methods of query, list () and iterate (), two methods are listed in the result set, they have 3 points different,
1: The returned type is different, list () returns list, iterate () returns iterator,
2: The way to get the data, the list () will be directly to the database, iterate () will 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, the ID is the condition to send a SQL to the database, so that if there is no data in the cache, The number of times the database is queried is n+1.

3:iterate will query the level 2 cache, and list will only query the first level cache.
Each object in the list returned in 4:list () is the original object, and the object returned in iterate () is the proxy object. (Debug can be found)

The difference between the hibernate-list () and the Iterate () method

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: 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.