I'm reiterating a post made at:
http://forum.hibernate.org/viewtopic.php?p=2378569 since the responses were inadequate and the question exposes a real problem with hibernate's 2nd level cache.
Post:
================
I have enabled second level cache for an entity. If I use entityManager.find to retrieve the entity by PK, the cache works (i.e. it hits the DB only the first time).
But if I make a HQL query on this entity, it hits the DB every time...
Here is the entity:
@Entity
@Table(name = "CORE_MessageBuyerTranslation", uniqueConstraints = {})
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region="com.ibx.ibxrequest.model.CoreMessageBuyerTranslation")
public class CoreMessageBuyerTranslation implements java.io.Serializable {
...
}
and here is the HQL that hits the DB every time:
List<CoreMessageBuyerTranslation> translatedKeys = entityManager.createQuery(
"from CoreMessageBuyerTranslation mbt "
+ "WHERE mbt.coreBuyer = :coreBuyer "
+ " AND mbt.coreLang = :coreLang "
+ " AND mbt.coreMessage.messageKey = :messageKey")
.setParameter("coreBuyer", buyer)
.setParameter("coreLang", lang)
.setParameter("messageKey", key)
.getResultList();
Why does it hit the DB every time? Shouldn't it use the cache?
=================
I would like to have a response from the developers since I believe this is a very severe limitation of the 2nd level cache which is causing huge performance problems for us in production since most of calls are hql or using the criteria interface. Also, why isn't there a session.getObjects? There's only getObject.