Hi,
I am trying to cache named queries in JPA, to cache the Named Query results for a particular time period of more than 20 minutes. I have written the ehcache.xml and defined the same reference in the persistence.xml, In the Entity option object, for the Named Query I have defined the Hints --> QueryHint = cacheable to true and the region.
But the query is being cached for only 2 minutes(120seconds). I would like to cache the query about 20 minutes.
Please find the below are the code in persistence.xml, ehcache.xml, POJO(Example: Option.java) and DAO(OptionDAO.java).
POJO(Option.java):
@NamedQuery(name = "findOptionsByLineOfBusinessAndStatus", query = "from Option where uniqueOptionKey.lineOfBusinessId= :lineOfBusiness AND optionStatus= :stateCode", hints = { @QueryHint(name = "org.hibernate.cacheable", value = "true"), @QueryHint(name = "org.hibernate.cacheRegion", value = "query.AdministrativeAreasPerCountry") })
persistence.xml:
<property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true"/> <property name="hibernate.generate_statistics" value="true"/> <property name="hibernate.cache.provider_configuration_file_resource_path" value="ehcache.xml"/> <!-- ehcache.xml is along with the persistence.xml in META-INF-->
ehcache.xml
<ehcache> <diskStore path="c:\\web\\"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="360000" timeToLiveSeconds="360000" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="360000" memoryStoreEvictionPolicy="LRU" />
<cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="360000" timeToLiveSeconds="360000" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="360000" memoryStoreEvictionPolicy="LRU" />
<cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="360000" timeToLiveSeconds="360000" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="360000" memoryStoreEvictionPolicy="LRU" />
<cache name="query.AdministrativeAreasPerCountry" maxElementsInMemory="500" eternal="false" timeToIdleSeconds="180000" timeToLiveSeconds="864000" overflowToDisk="true"/> </ehcache>
OptionDAO:
Query query = getEntityManager().createNamedQuery("from Option where uniqueOptionKey.lineOfBusinessId= :lineOfBusiness AND optionStatus= :stateCode)"; query.setParameter(LINE_OF_BUSINESS, lineOfBusiness); query.setParameter(STATE_CODE, stateCode);
options = (List<Option>) query.getResultList();
Could you please provide some information on the same. where exactly is missing the configuration properties being set are overridden to the default. The query being cached for 2 minutes is the default time to cache.
Thanks and Regards, Jayadev S
|