Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Hibernate 4 and Ehcache higher than 2.4.3

DZone's Guide to

Hibernate 4 and Ehcache higher than 2.4.3

Free Resource

SignalFx is the only real-time cloud monitoring platform for infrastructure, microservices, and applications. The platform collects metrics and traces across every component in your cloud environment, replacing traditional point tools with a single integrated solution that works across the stack.

For those using Hibernate 4, if you want to use Ehcache, you must add a specific dependency: hibernate-ehcache

But there’s a catch, it embeds Ehcache 2.4.3, which is unconvenient as the latest version of Ehcache is 2.7.0

So if you are looking to use Hibernate 4, you should exclude the embeded Ehcache dependency.

In your pom.xml, it will look like this:

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

<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-ehcache</artifactId>
 <version>4.1.9.Final</version>
 <exclusions>
 <exclusion>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache-core</artifactId>
 </exclusion>
</exclusions>

</dependency>

Then according to the version of ehcache, you will need either ehcache or ehcache-core

Up to 2.6.x:

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

Starting at 2.7.0:

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

Changes in your code:

Please note that Ehcache contains classes to support Hibernate 3, but  hibernate-ehcache contains classes for Hibernate 4

Meaning that in your classpath, you’ll end up with two packages:

For Hibernate 3, classes are in the package : net.sf.ehcache.hibernate.*

But for Hibernate 4, classes are in the package : org.hibernate.cache.ehcache.*

So your hibernate configuration will need these properties to be set up:

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

Please note that it is: org.hibernate.cache.ehcache.EhCacheRegionFactory while for Hibernate 3, it was net.sf.ehcache.hibernate.EhCacheRegionFactory

So be really careful in your code, you could have

import net.sf.ehcache.hibernate.*;

which will compile but then fail at run time since this is for Hibernate 4. Whatever hibernate class you’re using, you should have:

import org.hibernate.cache.*;

The exception is for the property net.sf.ehcache.configurationResourceName that will contain the path to your ehcache xml config file, just like in Hibernate 3

Similarly, just like for Hibernate 3, you will need:

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">false</property>

Eventually it would make sense that the Hibernate folks have a dependency on ehcache instead of embedding it in their hibernate-ehcache dependency, that would make things easier.





SignalFx is built on a massively scalable streaming architecture that applies advanced predictive analytics for real-time problem detection. With its NoSample™ distributed tracing capabilities, SignalFx reliably monitors all transactions across microservices, accurately identifying all anomalies. And through data-science-powered directed troubleshooting SignalFx guides the operator to find the root cause of issues in seconds.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}