In Hibernate version 3.X it's possible to configure 2 level cache in hibernate.cfg like this:
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
In version 4.3.0 there is no class EhCacheProvider in org.hibernate.cache package.
What is the workaround for this situation?
Thanks
STEP 1 Add EHcache dependency
Hibernate ships with the ehcache library
1.1] Maven Dependency
add maven dependency for Ehcache in your application as
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>[2.0.0]</version>
<type>pom</type>
</dependency>
1.2] Download Jar file
If you are not using maven dependency you can download the jars file from Download URL
add this jar file into lib directory and your project CLASSPATH.
STEP 2 Configuring EhCache
To configure ehcache, you need to do two steps:
2.1] configure Hibernate for second level caching
<property key="hibernate.cache.use_second_level_cache">true</property>
2.2] specify the second level cache provider
Hibernate 3.3 and above
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
Hibernate 3.2 and below
<property name="hibernate.cache.region.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
hope this will help you !
Add below depenedency in your pom.
<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>
Refer to this link: https://dzone.com/articles/hibernate-4-and-ehcache-higher
Related
I'm using spring-boot-starter-web:jar:2.6.2 in my project, this jar uses hibernate-validator:6.2.0.Final. Recently we decided to move to Hibernate 7.0.2.Final, so I added the following maven config:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${rest.starter.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.2.Final</version>
</dependency>
But now I get the following error during integration testing or run:
javax.validation.NoProviderFoundException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
Why does it happen and how to fix it?
spring-boot-2.x uses javax.validation Annotations and so you need an implementation for javax.validation.spi.ValidationProvider.
hibernate-validator:7.0.2 has moved from javax.validation to jakarta.validation and for that it is not downward compatible.
It depends on your project now:
upgrade to spring-boot 3.x ( also upgrade from javax.* to jakarta.* - this might be a huge upgrade )
or downgrade hibernate-validator to version 6.x
I hope I have explained it well :-)
I'm using Matlab MCR in web project so I imported these dependecies to pom.xml
<!-- Matlab client tool library -->
<!-- <dependency>
<groupId>DataConcatenation</groupId>
<artifactId>DataConcatenation</artifactId>
<version>0.0.5-SNAPSHOT</version>
</dependency> -->
<!-- <dependency>
<groupId>DataConcatenator</groupId>
<artifactId>DataConcatenator</artifactId>
<version>0.0.5-SNAPSHOT</version>
</dependency> -->
<!-- <dependency>
<groupId>DataConversion</groupId>
<artifactId>DataConversion</artifactId>
<version>0.0.5-SNAPSHOT</version>
</dependency> -->
<dependency>
<groupId>DataConverter</groupId>
<artifactId>DataConverter</artifactId>
<version>0.0.5-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>DataConcatenation</artifactId>
<groupId>DataConcatenation</groupId>
</exclusion>
<exclusion>
<artifactId>DataConcatenator</artifactId>
<groupId>DataConcatenator</groupId>
</exclusion>
<exclusion>
<artifactId>DataConversion</artifactId>
<groupId>DataConversion</groupId>
</exclusion>
</exclusions>
</dependency>
The first problems is that I have to exclude the other tree dependencies even if I use only DataConverter, But I need only dataConverter and this library doesn't have other dependecies.
The second and most important problem is this error:
Threw exception in ZipAndMat::createZipAndMat: java.lang.UnsatisfiedLinkError: Native Library /usr/v81/bin/glnxa64/libnativedl.so already loaded in another classloader
I read a lot of guide and I understand that I have to put this jar into $CATALINA_HOME/shared/lib so all the class loader share the same jar.
But how can I add this dependecy to the above path? This is the first time that I have to implement this configuration. I use tomcat on my server and deploy project through war file.
Thanks
You can place the jar into $CATALINA_HOME/shared/lib, and then specify in your POM the dependency's <scope> as provided:
<dependency>
<groupId>DataConverter</groupId>
<artifactId>DataConverter</artifactId>
<version>0.0.5-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
...
</exclusions>
</dependency>
This method is often used for jars which contain JDBC drivers when running on Tomcat; the jdbc jar is placed in $CATALINA_HOME/shared/lib (so all WARs can find it), yet each project list’s the JDBC jar as a dependency with provided as the scope.
See this post for more information on provided.
I started to learn Hibernate framework from "Hibernate 3.2 in Simple Steps" book, but I downloaded latest version of Hibernate which is 4.1.9. According to the book there are many essential jars we need to add to the class path like
Anttr-2.7.6.jar
asm.jar
asm-attrs.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
.
.
. etc.
But I can't find all those required jars in new version. So what can I do without going for an old version ?? Do I just only need to add jars in required folder,,, can any one please tell me what jars I should need to add class path (4.1.9 version).
This question may be silly but I'm a beginner and I'm stuck here. Please help me.
Thank you!
antlr-2.7.7.jar
commons-collections-3.2.1.jar
dom4j-1.6.1.jar
javassist-3.12.1.GA.jar
hibernate-core-4.0.1.Final.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jboss-logging-3.1.0.CR2.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
The zip file which you download will have a required folder inside lib.It will be having all the necessary jars.
This is well explained in the Hibernate documentation (never just depend on a book when there's an abundance of good online material available, as is the case with Hibernate):
The lib/required/ directory contains all the JARs Hibernate requires.
All the jars in this directory must also be included in your project's
classpath.
All jars you need are right there, in the .zip file you already downloaded.
Assume you are downloaded Hibernate Software( http://hibernate.org/ ) following location.
Hibernate_HOME = /home/rangareddy/Hibernate/hibernate-release-4.2.7.Final
Jar files:
%Hibernate_HOME%/lib/required/antlr-2.7.7.jar
%Hibernate_HOME%/lib/required/dom4j-1.6.1.jar
%Hibernate_HOME%/lib/required/hibernate-commons-annotations-4.0.2.Final.jar
%Hibernate_HOME%/lib/required/hibernate-core-4.2.7.Final.jar
%Hibernate_HOME%/lib/required/hibernate-jpa-2.0-api-1.0.1.Final.jar
%Hibernate_HOME%/lib/required/javassist-3.18.1-GA.jar
%Hibernate_HOME%/lib/required/jboss-logging-3.1.0.GA.jar
%Hibernate_HOME%/lib/required/jboss-transaction-api_1.1_spec-1.0.1.Final.jar
%Hibernate_HOME%/lib/jpa/hibernate-entitymanager-4.2.7.Final.jar
I'd suggest learn to use maven or gradle first and let them to help you set up the dependencies.
Using a dependency management tools in such days of java development is important and useful, all well known java libraries support this way and also it is recommanded.
A tech book may be outdated very soon, for such of this kind of details, the official doc would be always the first choice.
in case of using maven add this code to your pom file
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.1.0.CR2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
I'm working with a spring project using hibernate and look to implement second-level cache using ehcache. I see a number of approaches to this:
spring-modules-cache which introduces the #Cacheable annotation
ehcache-spring-annotations a toolset which aims to be the successor of spring-modules-cache.
Hibernate cache is nicely integrated into hibernate itself to perform caching using e.g., the #Cache annotation.
Programmatic cache using proxies. Annotation based config quickly becomes either limited or complex (e.g., several levels of annotation nesting)
Personally I don't think spring-modules-cache is thorough enough, hence I would probably prefer consider the more actively developed ehcache-spring-annotations. Hibernate cache though seems to be most complete implementation (e.g., both read and write cache etc).
What would motivate which toolset to use? Please share your caching experience ...
Our project uses option 3. We apply annotation org.hibernate.annotations.Cache to entities that we cache in an Ehcache, configure Ehcache using ehcache.xml, and enable and configure the Hibernate second-level cache in hibernate.cfg.xml:
<!-- Enable the second-level cache -->
<property name="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider
</property>
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.EhCacheRegionFactory
</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<property name="hibernate.cache.generate_statistics">true</property>
For most entities, we use cache concurrency strategy CacheConcurrencyStrategy.TRANSACTIONAL:
#Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
Our Maven project uses Hibernate 3.3.2GA and Ehcache 2.2.0:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
<exclusions>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.2.1.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
<version>3.3.2.Beta1</version>
</dependency>
Spring 3.1 has a new built-in cache abstraction. Read here.
Is it ok to take it from Glassfish project ( glassfish-persistence-api) or may be there is a Hibernate jar?
If you are using maven, adding below dependency should work
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
If you are developing an OSGi system I would recommend you to download the "bundlefied" version from Springsource Enterprise Bundle Repository.
Otherwise its ok to use a regular jar-file containing the javax.persistence package
You can use the ejb3-persistence.jar that's bundled with hibernate. This jar only includes the javax.persistence package.
In the latest and greatest Hibernate, I was able to resolve the dependency by including the hibernate-jpa-2.0-api-1.0.0.Final.jar within lib/jpa directory. I didn't find the ejb-persistence jar in the most recent download.
hibernate.jar and hibernate-entitymanager.jar contains only the packages org.hibernate.*. So you should take it from the Glassfish project.
For JPA 2.1 the javax.persistence package can be found in here:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
See: hibernate-jpa-2.1-api on Maven Central
The pattern seems to be to change the artefact name as the JPA version changes. If this continues new versions can be expected to arrive in Maven Central here: Hibernate JPA versions
The above JPA 2.1 APi can be used in conjunction with Hibernate 4.3.7, specifically:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.7.Final</version>
</dependency>
In general, i agree with above answers that recommend to add maven dependency, but i prefer following solution.
Add a dependency with API classes for full JavaEE profile:
<properties>
<javaee-api.version>7.0</javaee-api.version>
<hibernate-entitymanager.version>5.1.3.Final</hibernate-entitymanager.version>
</properties>
<depencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
Also add dependency with particular JPA provider like antonycc suggested:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-entitymanager.version}</version>
</dependency>
Note <scope>provided</scope> in API dependency section: this means that corresponding jar will not be exported into artifact's lib/, but will be provided by application server. Make sure your application server implements specified version of JavaEE API.