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