user10398
user10398

Reputation: 480

jpa hibernate and 2nd level cache

my configuration is as follows

persitence.xml

<property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/>
<property name="hibernate.ejb.classcache.demo.entities.UserID" value="read-only"/>
<property name="javax.persistence.sharedCache.mode" value="ALL"/>

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
    <diskStore path="java.io.tmpdir/Customer-portlet-cache"/>

    <defaultCache eternal="false" maxElementsInMemory="1000"
                  overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
                  timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>
    <cache name="userCache" eternal="true"
           maxElementsInMemory="999" overflowToDisk="true"
           diskPersistent="true" timeToIdleSeconds="0"
           timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU"/>
</ehcache>

I have a Seam Component annotated @Create which gets a listing for all the UserID and stores them away into the cache. The idea here is to get the application up with a warm cache, but when I access the entities from the application I start getting the Lazy initialization exceptions allover (Because the entity is not associated with a persistenceContext and has some OneToMany and ManyToMany Relations and they would not load with the entity and if i set the fetchtype to eager i get to more nasty areas)

Is there a way or a workaround for getting a warm cache when the users accesses the applications.

Upvotes: 0

Views: 1002

Answers (1)

Bozho
Bozho

Reputation: 597016

Don't manually access the 2nd level cache. If you need a separate caching layer, make a new one, different from hibernate's.

In any case, you can Hibernate.initialize(..) your entities and collections before putting/sending them anywhere

And finally, I don't think your 2nd level cache is configured properly. For newer version it should look like this:

<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.region.factory_class"
            value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" />

Upvotes: 1

Related Questions