无法实例化RegionFactory

时间:2012-09-25 07:31:48

标签: java hibernate jpa-2.0

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

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>

异常的Stacktrace是:

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 个答案:

答案 0 :(得分:5)

而不是:

<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 {
  // ...
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.6.0</version>
</dependency>

答案 3 :(得分:0)

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

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

它将解决问题。