8

我正在尝试启用二级缓存,但出现异常。

persistence.xml 中二级缓存的代码如下:

            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.EhCache"/>
        </properties>

异常的堆栈跟踪是:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.org.JPAUtil.<clinit>(JPAUtil.java:21)
    at com.org.Main.main(Main.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2404)
    at java.lang.Class.getConstructor0(Class.java:2714)
    at java.lang.Class.getConstructor(Class.java:1674)
    at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:409)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:280)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at com.org.JPAUtil.<clinit>(JPAUtil.java:17)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: net.sf.ehcache.CacheException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 20 more

为ehcache添加jar后,我得到了异常

Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.EhCache]
at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:423)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:280)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)
... 10 more
Caused by: java.lang.InstantiationException: org.hibernate.cache.EhCache
    at java.lang.Class.newInstance0(Class.java:357)
    at java.lang.Class.newInstance(Class.java:325)
    at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:419)
    ... 15 more
4

4 回答 4

6

代替:

<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.EhCache"/>

采用:

<property name="hibernate.cache.provider_class"  value="org.hibernate.cache.EhCacheProvider"/>

例如,使用 Hibernate 4.x 以及 JAR 文件hibernate-ehcache-4.3.10.Final.jarslf4j-api-1.6.1.jar,修改persistence.xml

<properties>
  <!-- ... other properties ... -->
  <property name="hibernate.show_sql" value="false"/>
  <property name="hibernate.format_sql" value="false"/>
  <property name="hibernate.cache.use_second_level_cache" value="true"/>
  <property name="hibernate.cache.provider_class"  value="org.hibernate.cache.EhCacheProvider"/>
</properties>

然后确保将hibernate-ehcache-4.3.10.Final.jarslf4j-api-1.6.1.jar部署到 JBoss。

不包括. _ehcache-core-2.4.3.jar

现在您可以注释实体,例如:

@Entity
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region = "region_name")
public class MyEntity implements Serializable {
  // ...
}
于 2012-09-26T04:29:46.643 回答
1

确保Hibernateehcache位于同一目录中。应用程序服务器 lib或您的应用程序WEB-INF/lib

于 2012-09-25T07:42:06.377 回答
0

检查是否ehcache-x.x.jar在您的类路径中。在 Eclipse 中,您可以通过right click your project -> build path -> configure build path. 如果你错过了那个 jar,你可以从这里下载。如果你使用的是 maven,你可以在你的 pom.xml 中加入以下依赖:

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.6.0</version>
</dependency>
于 2012-09-25T07:56:07.670 回答
0

在您的 persistence.xml 中使用以下属性。

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

它将解决问题。

于 2020-04-14T09:27:19.660 回答