Java – Hibernate 2nd level cache

annotationsehcachehibernatejava

Hi I've run into some problems with hibernate 2nd level cache.
As cache provider I use ehcache.

Part of config from persistence.xml

<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
<property name="hibernate.cache.provider_configuration_file_resource_path" value="/ehcache.xml" />

I configure my entities using annotations so:

@Cache(region = "Kierunek", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Kierunek implements Serializable {

imports for those annotations are:
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

my ehcache.xml

<diskStore path="java.io.tmpdir" />

<defaultCache maxElementsInMemory="10000" eternal="false"
    timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
    diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
    diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
    memoryStoreEvictionPolicy="LRU" />

<cache name="Kierunek" maxElementsInMemory="1000"
    eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />

And anyone idea why i get following error ?

WARNING: Could not find a specific ehcache configuration for cache named [persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB.Kierunek]; using defaults.
19:52:57,313 ERROR [AbstractKernelController] Error installing to Start: name=persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB state=Create
java.lang.IllegalArgumentException: Cache name cannot contain '/' characters.

solution is to add another property to persistence.xml

<property name="hibernate.cache.region_prefix" value=""/>

and that removes that faulty prefix big thx ruslan!

Best Answer

IMHO, you get the generated region name for your class. This generated name "persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB.pl.bdsdev.seps.encje.Kierunek". And it's not defined in your's ehcache.xml configuration. Also it's looking for the predefined name, so it can't use default region.

As an option to solve this problem you can use @Cache annotation properties to predefine some region name, like

@Cache(region = 'Kierunek', usage = CacheConcurrencyStrategy.READ_WRITE) 
public class Kierunek implements Serializable {
  // ....
}

And in ehcache.xml

<cache name="Kierunek" 
       maxElementsInMemory="1000"
       eternal="true" 
       overflowToDisk="false" 
       memoryStoreEvictionPolicy="LRU" />
Related Topic