Does HQL query always hit database and get results?
Asked Answered
S

2

6

I was going through hibernate and situations when to use Criteria vs HQL and my understanding is that with Hibernate, everytime when we are querying database either by Criteria or HQL in both instances hibernate would get result set and put in memory and then when we call that query again, data would be fetched from memory rather then hitting that database, is my understanding correct?

Also as you can see from comments to question mentioned below, it was suggested that Hibernate Criteria would get data from session and HQL would always go and hit database and so any number of multiple calls to HQL query will go and hit database and if this is the case then HQL causes more problems than solving.

Kindly advise on this as am little bit confused with the situation.

Reference to question

Systematology answered 24/1, 2012 at 15:39 Comment(0)
L
7

It depends on what kind of queries you are making and about your cache settings.

Hibernate has three kind of caches: session cache, query cache and 2nd level cache. Session cache is always on but the other two can be disabled.

Usually the caching is not the reason to favor Criteria API over HQL or vice versa. They are mostly just different interfaces for essentially the same thing.

See http://www.javalobby.org/java/forums/t48846.html and http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html

Ligulate answered 24/1, 2012 at 15:42 Comment(8)
AFAIK a HQL query will only use the query cache and only get the results from there if the query and all parameters are the same.Bethanie
so if am making "from " + test.class.getName() + SORT_BY_ID;, how can i decide if it is hitting memory or database, also how can we manage cache settings in hibernate v3.2?Systematology
@Juha: so now when you say session is by default always on then HQL should get result back from session right and not hit database again right?Systematology
@Systematology see my answer below. The session cache is always on but queries will never go to the session cache. Executing a query will trigger a flush and a database hit. It might hit the query cache, but that's another matter.Dermatoid
@Alex:OK, but how can i make sure that query get's resultset from memory only and not from database, all i want to do here is reduce database hits as it affects performance pretty heavily. Any ideas?Systematology
If you want to use a Query and not hit the database you'll need to use the Query Cache. The docs are here docs.jboss.org/hibernate/core/4.0/manual/en-US/html/…Dermatoid
You'll need both the query cache and 2nd level cache to prevent database access.Alina
@JuhaSyrjälä: Why do i need to have both query cache and 2nd level cache activated to prevent database access, will not query cache prevent database hit, is there any specific reference that I should be looking at for this understanding?Systematology
D
2

Basically if you're generating queries you're probably going to hit the database, the exception to this is if you've cached the query and parameters.

Hibernate queries (whether you use Criteria or HQL) will only return entities from the session cache (first level cache) if you get it with the @Id.

In order to cache a query you can use the following syntax:

session.createQuery("from X as x").setCacheable(true);

Edited for comments:

A query is not the same as a get with @Id. To get an object by its @Id you would write something like:

Entity myEntity = sessionFactory.getCurrentSession().get(Entity.class, 1);
Dermatoid answered 24/1, 2012 at 16:1 Comment(3)
can you give an example of hibernate queries which gets it with @IdSystematology
Something like this uses HQL to fetch with primary key (@Id) session.createQuery("from Entity e where e.id = ? ").setParameter(1, id)Alina
This is not going to hit the session cache. You're creating a query which will bypass the session cache and go straight to the database. If you want to fetch by @Id use Load or Get on the session.Dermatoid

© 2022 - 2024 — McMap. All rights reserved.