Upgrade from Hibernate 3.5 to Hibernate 5.5.x Latest Issues

Solution for Upgrade from Hibernate 3.5 to Hibernate 5.5.x Latest Issues
is Given Below:

My latest project is converting a Java Web-based app from Hibernate 3.5.6-Final to the latest Hibernate 5.5.4.Final. This app does not use Spring (yet) and so right now, it creates it’s own JDBC connection to the database.

Here are the latest Maven dependencies:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.5.4.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.5.4.Final</version>
</dependency>

When I updated the pom.xml, of course it created a lot of errors and warnings, and the errors are the first thing I am going to fix. I went through all the code and fixed those issues by replacing old classes with new classes and adding unimplemented classes, and remove calls to methods that aren’t needed anymore. The code now compiles just fine, however, when I run the app, I get this error message:

java.lang.NoClassDefFoundError: org/hibernate/cache/access/AccessType
at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166)
at java.base/java.lang.Class.privateGetPublicMethods(Class.java:3191)
at java.base/java.lang.Class.getMethods(Class.java:1904)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.applyInjections(AbstractServiceRegistryImpl.java:292)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:283)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:718)
at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:123)
at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:158)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:673)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:744)
at com.ekotrope.server.services.db.service.PersistenceManager.buildSessionFactory(PersistenceManager.java:508)

I know that our code is the last line of the error message. That line is:

 SessionFactory sessionFactory = config.buildSessionFactory();

and that goes deep into the Hibernate code as you can see in the error message, and finally comes down to:

 java.lang.NoClassDefFoundError: org/hibernate/cache/access/AccessType

I know this class is from Hibernate 3.5, and I have no idea why this would come up. I know the correct class would be:

    org.hibernate.cache.spi.access.AccessType

I have checked the Maven dependencies to see if there is any other Hibernate jars in the mix, and I don’t see any. So, I am looking to fix this, and any help would be appreciated.

The issue was the old ehcache that we had when using Hibernate 3.

  1. We did not have any old hibernate-ehcache defined in the pom.xml, and we didn’t need it at the time with Hibernate 3.

  2. We were using the old ehcache as defined in maven as:

    <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.11</version>
    </dependency>
    

With this, the primary-hibernate.cfg.xml had the following setup for the second level Hibernate cache.

<property name="cache.provider_class">
    net.sf.ehcache.hibernate.EhCacheProvider
</property>
<property name="hibernate.cache.region.factory_class">
    net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
</property>

Well, we not only updated from Hibernate 3 to the latest Hibernate 5, and added the latest ‘hibernate-ehcache’, but we also updated to this EhCache as shown in the Maven pom.xml.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.5.5.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>5.5.5.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.5.5.Final</version>
</dependency>

 <dependency>
      <groupId>org.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>3.8.1</version>
</dependency>  

With this, we modified our primary-hibernate.cfg.xml for the second level cache as follows.

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

We did scour our code to make sure we replaced all the occurrences of ‘net.sf.ehcache’ to the newer ehcache classes.

Now, I can build and compile my code and this works.