java.lang.NoClassDefFoundError: org/hibernate/cache/EntityRegion configuring EHCache

46,472

Solution 1

ehcache-core files are basically for Hibernate 3.x. Hibernate 4.x comes with its own implementation for ehcache. You don't need to use ehcache explicitly in hibernate 4.x. Here is the best answer for your problem.

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

Solution 2

I checked maven the dependencies. Maven always loads ehcache-code when the hibernate-ehcache package is loaded.

Dependency tree:

+--- 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

Please check your persistence configuration. It should not include any classes where the package name starts with "net.sf"

For example, you have to replace the following string in your code:

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

with the following string:

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

Solution 3

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

For Hibernate 4, use

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

Hibernate Configuration Article

Solution 4

From Hibernate 4, there s new hibernate-cache introduce for caching. This need latest dependency for net.sf.ehcache upto 2.6. Following article will CLICK ON LINK. When i add following dependency in my pom, the error will remove:

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.8.3</version>
    </dependency>
Share:
46,472

Related videos on Youtube

Dave A
Author by

Dave A

Updated on August 23, 2020

Comments

  • Dave A
    Dave A over 3 years

    I'm trying to add ehcache (v2.6.0) to my Hibernate 4.1.5.SP1 project, but having some configuration issues. Specifically, I'm getting a java.lang.NoClassDefFoundError: org/hibernate/cache/EntityRegion error when I try and build my Hibernate configuration with

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

    Here's my Maven dependencies ...

        <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>
    

    Here's the Java code I use to configure it ...

        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);
    

    and here's the hideous error. WHat configuration am I missing?

    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
    
  • Mariusz
    Mariusz about 11 years
    The problem with this approach (using hibernate-ehcache dependency instead of ehcache-core) is this: As of time of writing this comment, the most recent version of hibernate-ehcache (4.2.0.CR2) depends on ehcache-core in version 2.4.3 according to mvnrepository.com. This is causing exception like "Element does not allow attribute "maxEntriesLocalHeap", because this attribute (similarly to new <persistence> inner elements, that you define in your ehcache.xml file) have been added just recently in Ehcache (2.6.x).
  • Mariusz
    Mariusz about 11 years
    The workaround is to exclude ehcache-core transitive dependency from hibernate-ehcache in your pom and add ver 2.6.x dependency on Ehcache instead.
  • Gorkamorka
    Gorkamorka over 10 years
    The page with the answer gives a 404, thus this answer is no longer valid.
  • OO7
    OO7 over 9 years
    @bluefeet♦ Thanks for updating broken link with working one. @Tapas +1 for link.
  • bvdb
    bvdb almost 9 years
    web archive kind find it (anymore). Link broken again.