Spring - Hibernate Integration - java

I am trying to integrate Spring 4 with Hibernate 4 and i am using eclipse ide with out maven dependencies,But i am getting below mentioned error:
Caused by: java.lang.classnotfoundexception : org.hibernate.annotations.Entity
if any one has implemented already and working fine, So please can you share the project for reference so that i can understand the flow of integration, As i am new to spring and hibernate.I need some good guidance.

you need to use some dependency, which you can find below :
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.5-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.9.Final</version>
</dependency>
and if you are not using maven project then you need to download the hibernate-entitymanager and hibernate-annotations jar and add into build path and rebuild the project

Related

Weblogic + Hibernate Error: javax.persistence.JoinTable.indexes()[Ljavax/persistence/Index

I'm migrating a project that was running on Tomcat 8 to Weblogic.
I am using Hibernate + JPA 2. When I deploy in weblogic got the following error:
Failure occurred in the execution of deployment request with ID "26012160125422" for task "114". Error is: "weblogic.application.ModuleException: java.lang.NoSuchMethodError: javax.persistence.JoinTable.indexes()[Ljavax/persistence/Index;"
weblogic.application.ModuleException: java.lang.NoSuchMethodError: javax.persistence.JoinTable.indexes()[Ljavax/persistence/Index
Environment
- Java: jdk1.8.0_60
- Weblogic: 12.1.3
- hibernate-entitymanager: 4.3.1.Final
- hibernate-core: 4.3.1.Final
- javaee-api: 7.0
pom.xml
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
It is strange that you use javaee-api-7.0.jar, Hibernate 4 has hibernate-jpa-2.1-api-1.0.0.Final.jar as a dependency. But it is not a reason of the error.
JoinTable.indexes() was added with Java Persistence 2.1. So you just have an old jar with JoinTable annotation in the classpath (in the Weblogic default lib folders).
To check where is this jar, run this code before the Hibernate configuration code
URL joinTableUrl = Thread.currentThread().getContextClassLoader()
.getResource(
"javax/persistence/JoinTable.class");
System.out.println(joinTableUrl);
To check JoinTable has indexes() method
Class<?> joinTable = Thread.currentThread().getContextClassLoader()
.loadClass(JoinTable.class.getName());
System.out.println(Arrays.asList(joinTable.getDeclaredMethods()));

java.lang.ClassNotFoundException: javax.persistence.Persistence cannot be found with JPA

I'm praticing with JPA API. I got an error
java.lang.ClassNotFoundException: javax.persistence.Persistence cannot be found
My code below:
EntityManagerFactory emf;
emf = Persistence.createEntityManagerFactory("mail");
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("SELECT v FROM Version v");
List<Version> versions = query.getResultList();
The error at line emf = Persistence.createEntityManagerFactory("mail");
Any solution?
You are trying to set up a standalone JPA project. In order to do so you need a JPA provider jars. The two more popular providers are Eclipselink and Hibernate. If you are using maven you can add dependencies to their implementations.
For Eclipselink
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
</dependency>
For Hibernate
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
If you are not using maven you can download their implementations from their sites and put it in your classpath.
For Eclipselink: http://www.eclipse.org/eclipselink/downloads/
For Hibernate: http://hibernate.org/orm/
Some JPA quickstarts are recommending to add only the JPA API (interface declarations only) dependencies with maven.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
or
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
This approach will be successful only in server environment as the server will provide appropriate implementation at runtime.
Right Click on Project -> Properties -> Search "Deployment Assembly" -> Add Maven Dependencies.
if using maven , add below dependency in Pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
then clean install maven

Not work maven dependencies in Spring MVC

I'm writting my application using Intellij IDEA, maven, Spring MVC. I've added maven dependencies in my pom.xml:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
</dependency>
But classes javax.validation and org.hibernate.validator cannot be found.
I try File->Invalidate Caches / Restart... in IntelliJ but it not works. All other dependencies work correct.
Try this:
right click on pom.xml
choose Maven -> Generate sources and update folders

Hibernate 4.1.9 : Which jar files do I need?

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>

What jar should I include to use javax.persistence package in a hibernate based application?

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.

Categories

Resources