I want to use second level cache for my entity, but in the application I am retrieving the entity by a field that is unique(setting name), not by the primary key. Problem is that the query does not use the cache.
Hibernate version:
[Version] Hibernate Annotations 3.3.0.GA
[Environment] Hibernate 3.2.4.sp1
[Version] Hibernate EntityManager 3.3.1.GA
I am using this code to query the DB:
Code:
session.createCriteria(CoreSystemSettings.class).add(
Restrictions.eq("name", settingName)
).uniqueResult()
The problem is that the above query ignores the second level cache that is enabled for CoreSystemSettings entity.
Is there a workaround for this, so we can use second level cache?
Entity definition:
Code:
@Entity
@Table(name = "CORE_SystemSetting", uniqueConstraints = {})
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region="com.ibx.ibxrequest.model.CoreSystemSetting")
public class CoreSystemSetting implements java.io.Serializable {
...
@Id
@Column(name = "SystemSettingId", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getSystemSettingId() {
return this.systemSettingId;
}
@Column(name = "Name", unique = false, nullable = false, insertable = true, updatable = true, length = 50)
public String getName() {
return this.name;
}
...
}
Excerpt from ehcache.xml:
Code:
<cache name="com.ibx.ibxrequest.model.CoreSystemSetting"
maxElementsInMemory="300"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
eternal="false"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
What solutions do I have in order to be able to use second level cache?
Thanks