DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

[DZone Research] Observability + Performance: We want to hear your experience and insights. Join us for our annual survey (enter to win $$).

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Spring + Hibernate + EhCache Caching
  • Auditing Spring Boot Using JPA, Hibernate, and Spring Data JPA
  • Marco Codes Live: Gavin King and Hibernate 6.3 [Video]
  • Postgres JSON Functions With Hibernate 6

Trending

  • Understanding Europe's Cyber Resilience Act and What It Means for You
  • Top 7 Best Practices DevSecOps Team Must Implement in the CI/CD Process
  • GenAI-Infused ChatGPT: A Guide To Effective Prompt Engineering
  • Auto-Scaling DynamoDB Streams Applications on Kubernetes
  1. DZone
  2. Coding
  3. Java
  4. Hibernate 4 and Ehcache higher than 2.4.3

Hibernate 4 and Ehcache higher than 2.4.3

Aurelien Broszniowski user avatar by
Aurelien Broszniowski
·
Mar. 15, 13 · Interview
Like (2)
Save
Tweet
Share
19.22K Views

Join the DZone community and get the full member experience.

Join For Free

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.





Hibernate Ehcache

Published at DZone with permission of Aurelien Broszniowski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring + Hibernate + EhCache Caching
  • Auditing Spring Boot Using JPA, Hibernate, and Spring Data JPA
  • Marco Codes Live: Gavin King and Hibernate 6.3 [Video]
  • Postgres JSON Functions With Hibernate 6

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: