2011-04-29 93 views
0

我的配置如下JPA Hibernate和2級高速緩存

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> 

我有一個Seam組件的註解@Create它獲取所有UserID的列表並將它們存儲到緩存中。這裏的想法是讓應用程序擁有一個溫暖的緩存,但是當我從應用程序訪問實體時,我開始全面獲取Lazy初始化異常(因爲實體不與persistenceContext關聯,並且具有一些OneToMany和ManyToMany關係,並且它們不會與實體一起加載,如果我將fetchtype設置爲渴望,我會到更加討厭的區域)

當用戶訪問應用程序時,有沒有一種方法或解決方法可以獲得熱緩存。

回答

1

不要手動訪問二級緩存。如果您需要單獨的緩存層,請創建一個與hibernate不同的新層。

在任何情況下,你可以把/把他們隨時隨地

之前Hibernate.initialize(..)您的實體與集合。最後,我不認爲你的2級高速緩存的配置是否正確。對於較新的版本,應該看起來像這樣:

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

是的singletonehcachefactory需要修復。 thatnks for that :-) – user10398 2011-04-29 07:59:28