Do java – HQL queries always hit the database and get results?
I'm going through hibernate and when to use criteria vs HQL. My understanding is to use hibernate. Whenever we query the database through criteria or HQL in two instances, hibernate will get the result set and put it into memory, and then query again when we call it. The data will be obtained from memory instead of clicking the database. Is my understanding correct?
In addition, as you can see in the comments and questions mentioned below, it is suggested that hibernate criteria will get data from the session, and HQL will always enter and hit the database. Therefore, any number of multiple calls to HQL queries will enter and hit the database. If this is the case, HQL will cause more problems than solving problems
Please advise on this, because I'm a little confused about this situation
Refer to question
Solution
This depends on the type of query you are working on and the cache settings
Hibernate has three kinds of cache: session cache, query cache and L2 cache The session cache is always on, but the other two can be disabled
In general, caching is not the reason for supporting criteria API instead of HQL, and vice versa For basically the same things, they are mostly just different interfaces
see http://www.javalobby.org/java/forums/t48846.html and http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html