(1) When querying with the query method, through the HQL statement to get the query object, and the query object operation, the first is to use the list method to obtain the list of the query of the merge output
Public void Listquery () {
Configuration configuration = new configuration (). Configure ();
Sessionfactory factory = Configuration.buildsessionfactory ();
Session session = Factory.opensession ();
Query query = Session.createquery ("from Customers");
list<customers> list = Query.list ();
for (Customers entity:list) {
System. out. println (Entity.tostring ());
}
}
The result of the output is:
The Execute SQL statement for the list is:
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 customers cus tomers0_
[Email protected]
(2) using query to get the object of query and output with iterator 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 ();
Iterate through all the query results
while (It.hasnext ()) {
Customers customer = It.next ();
System. out. println (Customer.tostring ());
}
}
Results of iterator execution:
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_, customers 0_.sex as sex0_0_, customers0_.petname as petname0_0_, Customers0_.email as email0_0_, customers0_.rdate as rdate0_0_ from Customers customers0_ where customers0_.id=?
[Email protected]
Conclusion:
(1) From the above implementation results can be seen to obtain different ways
List is obtained by:list<customers> list = Query.list ();
Iterator Acquisition method:iterator<customers> it = Query.iterate ();
(2) from the execution results can be seen in the list output a statement, and iterator output is two SQL statements, we can think about, why output such an effect?
Because they get the data in a different way, the list () will directly query the database, iterator () will first 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, with the ID condition and then send a SQL to the database, So if there is no data in the cache, the number of times to query the database is n+1
(3) The list only queries the first level cache, and the iterator is checked from the level two cache.
(4) The object returned by the list method is an entity object, and iterator returns a proxy object
(5) The second time the list is issued in the session, the database will still be consulted
(6) Iterate the second time, first find session level cache
The difference between list and iterator