When hql is used in Hibernate for query, list or Iterator is usually used to obtain the query results. Here, we will briefly describe the differences and applicability of the two methods.
When hql is used in Hibernate for query, list or Iterator is usually used to obtain the query results. Here, we will briefly describe the differences and applicability of the two methods.
When hql is used in Hibernate for query, list or Iterator is usually used to obtain the query results. Here we will briefly describe the differences between the two methods and applicable scenarios.
List: The queried results will be fully stored in the memory (session cache), and the object will be in a persistent state. if the fetch mode is used, a record corresponding to the master table may exist in multiple instances in the list (instance reference is accurate ), appears multiple times but points to the same object. list only executes one SQL statement.
Iterator: queries all IDs for the first time, and then determines whether to query the database based on whether the corresponding object exists in the session cache during the real Iterator. if the session cache does not exist, one or more SQL queries are usually generated.
List example:
S1 = sessionFactory. openSession ();
Query q = s1.createQuery ("from com. test. hb. Hbtest h where h. id <2 ");
List l = q. list ();
Iterator itr = l. iterator ();
While (itr. hasNext ()){
Hbtest tbo = (Hbtest) itr. next ();
One SQL statement:
Select hbtest0 _. ID as ID0 _, hbtest0 _. VAL as VAL0 _ from HB. HBTEST hbtest0 _ where hbtest0 _. ID <2
Iterator example:
S1 = sessionFactory. openSession ();
Query q = s1.createQuery ("from com. test. hb. Hbtest h where h. id <2 ");
Iterator itr = q. Iterator ();
While (itr. hasNext ()){
Hbtest tbo = (Hbtest) itr. next ();
3 SQL statements (1 + 2)
Obtain the id of the first SQL statement
Select hbtest0 _. ID as col_0_0 _ from HB. HBTEST hbtest0 _ where hbtest0 _. ID <2
Two SQL statements that retrieve records based on id (records corresponding to the id do not exist in the cache)
Select hbtest0 _. ID as ID0_0 _, hbtest0 _. VAL as VAL0_0 _ from HB. HBTEST hbtest0 _ where hbtest0 _. ID =?
Use Case: If no corresponding record exists in the cache, use the list method. If a large number of records already exist in the cache, Iterator provides better performance.
Impact of the cache of the query on the list and iterator modes:
When you open the query cache, the query itself is cached., And add parameters and paging information as the key value,Instead of the query result. The query result is stored in the session cache, which is different from the query cache.
Open query cache: 1, set hibernate. cache. use_query_cache = true, 2 in config, and query. setCacheable (true) after the query is created );
The following figure shows how to useListScenarios:
1:
Query q1 = s1.createQuery ("from com. test. hb. Hbtest h where h. id <3 ");
Q1.setCacheable (true );
Query q2 = s1.createQuery ("from com. test. hb. Hbtest h where h. id = 1 ");
Q2.setCacheable (true );
Logically, the value of the first query should contain the second query, but the second query will not use the query cache, but will query the database again.
2:
Query q1 = s1.createQuery ("from com. test. hb. Hbtest h where h. id = 1 ");
Q1.setCacheable (true );
Query q2 = s1.createQuery ("from com. test. hb. Hbtest h where h. id = 1 ");
Q2.setCacheable (true );
The two queries are identical. The second query uses the query cache and does not query the database again.
3:
Query q1 = s1.createQuery ("from com. test. hb. Hbtest h where h. id =? "). SetInteger (0, 1 );
Q1.setCacheable (true );
Query q2 = s1.createQuery ("from com. test. hb. Hbtest h where h. id =? "). SetInteger (0, 1 );
Q2.setCacheable (true );
The two queries are the same. The second query uses the query cache and does not query the database again.
4,
Query q1 = s1.createQuery ("from com. test. hb. Hbtest h where h. id =? "). SetInteger (0, 1 );
Q1.setCacheable (true );
Query q2 = s1.createQuery ("from com. test. hb. Hbtest h where h. id =? "). SetInteger (0, 2 );
Q2.setCacheable (true );
Logically, two queries are different. The second query will not use the query cache, but will query the database again.
UseIteratorScenario:
The query cache does not cache the first ID-specific SQL statement. Later, the iterator determines the database access based on the cache in the session.
The iterator method is not affected.
** If the cache is native SQL, the SQL statements of the two queries must be the same (including case sensitivity). The following example shows how to access the database twice.
Query q1 = s1.createSQLQuery ("select val from Hbtest H where H. id = 1 ");
Query q2 = s1.createSQLQuery ("select val from Hbtest h where h. id = 1 ");
However, HQL does not. HQL is translated into SQL by hibernate. In this process, it will identify whether the same SQL statement is used. The following example only accesses the database once.
Query q1 = s1.createQuery ("from com. test. hb. Hbtest h1 where h1.id = 1 ");
Query q2 = s1.createQuery ("from com. test. hb. Hbtest h where h. id = 1 ");