1

This blog post (referenced here) says that SQLQuery does not use the Hibernate session cache. However, a) there's no evidence, b) they don't use SQLQuery.addEntity, which might give information allowing Hibernate to use its cache.

The docs don't mention caching one way or another.

Does SQLQuery use the session cache if I use addEntity?

Edit: This section of the docs says:

Whenever you pass an object to save(), update() or saveOrUpdate(), and whenever you retrieve an object using load(), get(), list(), iterate() or scroll(), that object is added to the internal cache of the Session.

Since addEntity() allows list() to return objects, that implies it is using the cache. How can I check? Those same docs say there is a "contains" method for the cache, and one can browse the second-level cache, but not how to browse the session cache.

Edit #2: By turning on logging of Hibernate queries, it does not seem to be caching, but perhaps some set of circumstances would make it cache.

2 Answers 2

2

I don't think beny23's answer is correct. I believe setCacheable refers to second level caching not session cache.

To check if an entity is in the session cache you can use Session.contains(). You could also do a quick test: fetch entity, modify it, close session and see if the mod is in the db.

Sign up to request clarification or add additional context in comments.

Comments

0

According to the javadocs for SQLQuery, it extends the Query interface, which provides the setCacheable method (false by default), so you should be able to do the following:

Query query = session.createSQLQuery(
  "select * from my_table e")
  .addEntity(MyEntityClass.class)
  .setCacheable(true);

Additionally, you may want to look into setting the cache region by using the setCacheMode and setCacheRegion methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.