【问题标题】:java.lang.NoClassDefFoundError: org/hibernate/cache/EntityRegion configuring EHCachejava.lang.NoClassDefFoundError: org/hibernate/cache/EntityRegion 配置 EHCache
【发布时间】:2012-08-07 23:53:10
【问题描述】:

我正在尝试将ehcache (v2.6.0) 添加到我的Hibernate 4.1.5.SP1 项目中,但遇到了一些配置问题。具体来说,当我尝试使用

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

这是我的 Maven 依赖项...

    <hibernate.version>4.1.5.SP1</hibernate.version>
    <hibernate.validator.version>4.3.0.Final</hibernate.validator.version>
    <ehcacheVersion>2.6.0</ehcacheVersion>
    ...
    <!-- Hibernate dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>${ehcacheVersion}</version>
    </dependency>

这是我用来配置它的 Java 代码...

    Configuration config = new Configuration()
        .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
        .setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
        .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:myprojectTestDb")
        .setProperty("hibernate.connection.username", "sa")
        .setProperty("hibernate.connection.password", "")
        .setProperty("hibernate.connection.pool_size", "1")
        .setProperty("hibernate.connection.autocommit", "true")
        .setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider")
        .setProperty("hibernate.hbm2ddl.auto", "create-drop")
        .setProperty("hibernate.show_sql", "true")
        .setProperty("hibernate.current_session_context_class", "thread")
        .setProperty("hibernate.cache.use_second_level_cache", "true")
        .setProperty("hibernate.cache.region.factory_class", "net.sf.ehcache.hibernate.EhCacheRegionFactory")
        .addAnnotatedClass(Organization.class)
        .addAnnotatedClass(State.class) 
        .addAnnotatedClass(Country.class)
        .addAnnotatedClass(Domain.class)
        .addAnnotatedClass(Community.class);
    final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
    sessionFactory = config.buildSessionFactory(serviceRegistry);

这是可怕的错误。我缺少什么配置?

java.lang.NoClassDefFoundError: org/hibernate/cache/EntityRegion
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:386)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:251)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2270)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2266)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1735)
    at org.mainco.subco.orgsclient.service.OrganizationServiceTest.setupOrgServiceTest(OrganizationServiceTest.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.EntityRegion
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 32 more

【问题讨论】:

    标签: java hibernate maven configuration ehcache


    【解决方案1】:

    ehcache-core 文件基本上适用于 Hibernate 3.x。 Hibernate 4.x 带有自己的 ehcache 实现。您不需要在 hibernate 4.x 中显式使用 ehcache。这是您问题的最佳答案。

    http://web.archive.org/web/20130117102553/http://www.javacraft.org/2012/03/migrate-to-hibernate-4-ehcache.html

    【讨论】:

    • 这种方法的问题(使用 hibernate-ehcache 依赖而不是 ehcache-core)是:截至撰写此评论时,最新版本的 hibernate-ehcache (4.2.0.CR2 ) 取决于mvnrepository.com 版本 2.4.3 中的 ehcache-core。这会导致诸如“元素不允许属性”maxEntriesLocalHeap”之类的异常,因为该属性(类似于您在 ehcache.xml 文件中定义的新 内部元素)最近才在 Ehcache (2.6.x) 中添加.
    • 解决方法是从 pom 中的 hibernate-ehcache 中排除 ehcache-core 传递依赖,并添加对 Ehcache 的 2.6.x 版本的依赖。
    • 有答案的页面给出了 404,因此这个答案不再有效。
    • @bluefeet♦ 感谢您使用工作链接更新断开的链接。 @Tapas +1 链接。
    • 网络存档类找到它(不再)。链接再次断开。
    【解决方案2】:

    我检查了 maven 的依赖关系。 Maven 总是在加载 hibernate-ehcache 包时加载 ehcache-code。

    依赖树:

    +--- org.hibernate:hibernate-ehcache:4.3.0.Final
    |    +--- org.jboss.logging:jboss-logging:3.1.3.GA
    |    +--- org.jboss.logging:jboss-logging-annotations:1.2.0.Beta1
    |    +--- org.hibernate:hibernate-core:4.3.0.Final (*)
    |    \--- net.sf.ehcache:ehcache-core:2.4.3
    |         \--- org.slf4j:slf4j-api:1.6.1 -> 1.6.6
    

    请检查您的持久性配置。 它不应包含包名以“net.sf”开头的任何类

    例如,您必须在代码中替换以下字符串:

    .setProperty("hibernate.cache.region.factory_class", "net.sf.ehcache.hibernate.EhCacheRegionFactory")
    

    使用以下字符串:

    .setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory")
    

    【讨论】:

      【解决方案3】:
      Configuration.setProperty("hibernate.cache.region.factory_class", 
                           "net.sf.ehcache.hibernate.EhCacheRegionFactory")
      

      对于 Hibernate 4,使用

      org.hibernate.cache.ehcache.EhCacheRegionFactory instead of net.sf.ehcache.hibernate.EhCacheRegionFactory
      

      Hibernate Configuration Article

      【讨论】:

        【解决方案4】:

        从 Hibernate 4 开始,为缓存引入了新的 hibernate-cache。这需要net.sf.ehcache 的最新依赖项,最高可达 2.6。以下文章将CLICK ON LINK。 当我在我的 pom 中添加以下依赖项时,错误将删除:

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

        【讨论】:

          猜你喜欢
          • 2015-10-28
          • 2015-06-05
          • 2012-06-24
          • 2017-05-11
          • 1970-01-01
          • 2011-11-30
          • 2013-02-11
          相关资源
          最近更新 更多