org.hibernate.cache.CacheException: 不支持的访问类型 [读写]

org.hibernate.cache.CacheException: Unsupported access type [read-write]

我正在尝试使用 EhCache 启用二级缓存,但在我的服务器启动时遇到问题。当我为我的 DTO class 指定 @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) 时,出现以下错误:

16:20:42,882 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014612: Operation ("deploy") failed - address: ([("deploy ment" => "PORTAL.ear")]) - failure description: {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"PORTAL.ear#AppPU\"" => "org.jb oss.msc.service.StartException in service jboss.persistenceunit.\"PORTAL.ear#AppPU\": javax.persistence.PersistenceException: [PersistenceUnit: AppPU] Unable to build EntityManagerFactory Caused by: javax.persistence.PersistenceException: [PersistenceUnit: AppPU] Unable to build EntityManagerFactory Caused by: org.hibernate.cache.CacheException: Unsupported access type [read-write]"}}

如果我使用 CacheConcurrencyStrategy.READ_ONLY,我不会遇到任何启动问题。

下面是我的persistence.xml:

<persistence-unit name="AppPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/myDS</jta-data-source>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
      <properties>
          <!-- Auto-detect entity classes -->
          <property name="hibernate.archive.autodetection" value="class, hbm"/>

          <!-- Print sql executed - useful for debugging -->
          <property name="hibernate.show_sql" value="false"/>
          <property name="hibernate.format_sql" value="false"/>

         <!-- Properties for Hibernate -->
         <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
          <property name="hibernate.cache.provider_class" value="net.sf.Ehcache.hibernate.SingletonEhcacheProvider"/>
         <!--  <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/> -->
        <property name="net.sf.Ehcache.configurationResourceName" value="/ehcache.xml"/>
        <property name="hibernate.cache.use_query_cache" value="true"/>
        <property name="hibernate.cache.use_second_level_cache" value="true"/>
        <property name="hibernate.generate_statistics" value="true"/>
       </properties>
   </persistence-unit>

我想使用READ_WRITE启用二级缓存。

有人可以帮我解决这个问题吗?提前致谢。

配置ehcache需要两步:

为二级缓存配置 Hibernate 指定二级缓存提供者

休眠 4.x 及以上

<property key="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

Hibernate 3.3 及更高版本

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

Hibernate 3.2 及以下版本

<property key="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>

reference