Showing posts with label Hibernate dependencies. Show all posts
Showing posts with label Hibernate dependencies. Show all posts

Monday, October 18, 2010

Maven and Hibernate hell.

I am a Maven newbie and wanted to setup an example Maven + Spring + Hibernate + Mockito project. I have been fairly successful at that but not without my struggles with Maven.

Here is my hibernate dependency structure in pom.xml. Some experienced maven and hibernate users probably would spot the mistake right away. Anyway, let me go on and fix the "errors" myself.


        
            org.hibernate
            hibernate-core
            3.6.0.CR2
        
        
            org.hibernate
            hibernate-annotations
            3.5.7-SNAPSHOT
        
        
            org.hibernate
            hibernate-commons-annotations
            3.3.0.ga
        
        
            org.hibernate
            hibernate-entitymanager
            3.6.0.CR2
        



Struggle #1: Maven and transitive dependencies.

I try to deploy my application in Tomcat and get the error below:

org.springframework.beans.factory.BeanCreationException: 
  Error creating bean with name 'sessionFactory' defined in 
  ServletContext resource [/WEB-INF/spring/hibernate-config.xml]: 
  Invocation of init method failed; nested exception is
  java.lang.IncompatibleClassChangeError: Implementing class


After researching I found that the reason is multiple hibernate jars in the classpath. I check and yes there is a: hibernate.3.2.1.ga.jar and b: hibernate-core-3.6.0.CR2.jar. So now I have the task of finding out which hibernate dependency is bringing the 2 different hibernate jars.

On further research I find that hibernate-commons-annotation.3.3.0.ga.pom has dependency on hibernate.3.2.1.ga and hibernate-annotations-3.5.7-SNAPSHOT.pom has dependency on hibernate-core-3.6.0.CR2.

So that explains why I see two jars. I manually exclude the hibernate jar from hibernate-commons-annotations:


            org.hibernate
            hibernate-commons-annotations
            3.3.0.ga
            
                
                
                    org.hibernate
                    hibernate
                
            
        


And that takes care of multiple hibernate jars on the classpath. Is there is an easy way to not have transitive dependency in maven?

Struggle #2: Now on deployment I get:

Caused by: java.lang.NoClassDefFoundError:
  org/hibernate/annotations/common/reflection/MetadataProvider


hibernate-commons-annotation.3.3.0.ga.jar does not have MetadataProvider class. After googling the error I find: http://forum.springsource.org/showthread.php?t=89693

From hibernate-commons-annotation readme:

Version '3.3.0.ga' was a mistake during the release process, please disregard.
There is not such version 3.3.0.ga.

The solution then is to use hibernate-commons-3.2.0.Final

Final working dependency structure:


        
            org.hibernate
            hibernate-annotations
            3.5.7-SNAPSHOT
        
        
        
            org.hibernate
            hibernate-commons-annotations
            3.2.0.Final
        
        
            org.hibernate
            hibernate-entitymanager
            3.6.0.CR2
        

I wonder if all of this effort would have been saved had I just downloaded the jars from hibernate.org directly. Which makes me wonder if Maven is worth it for small projects?

Sunday, October 17, 2010

Add hibernate related dependencies to pom.xml

To be able to use Hibernate in your project you need reference to Hibernate jars. Once you have set up your Maven to access JBoss repository for Hibernate artifacts, you can add the dependencies below to your pom.xml:

We have added:
#1. Hibernate annotations.
#2. Hibernate commons annotations.
#3. Hibernate entity manager.
#4. Hibernate validator.


  
    org.hibernate
    hibernate-annotations
    3.5.7-SNAPSHOT
  

  
  
    org.hibernate
    hibernate-commons-annotations
    3.2.0.Final    
  
  
    org.hibernate
    hibernate-entitymanager
    3.6.0.CR2
  

  
  
     javax.validation
     validation-api
     1.0.0.GA
  
  
    org.hibernate
    hibernate-validator
    4.0.2.GA
  

Maven, Spring MVC, Hibernate and Mockito

In this 8 part series, I will cover how to create a simple Maven Web project, and then add Hibernate, Spring MVC and Mockito dependencies. I am assuming you have Maven installed. If not you can learn how to install Maven here.

First we will start with a brand new Maven project:

Step #1: Generate a Maven Webapp project:

mvn archetype:generate

The command above provides us a lot of archetype options. For this demo, we are going to choose #83 that is: maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.).

Below (in bold) were the options I chose:

Choose a number: 80: 83
Choose version:
1: 1.0
2: 1.0-alpha-1
3: 1.0-alpha-2
4: 1.0-alpha-3
5: 1.0-alpha-4
Choose a number: : 5
Define value for property 'groupId': : com.mayabansi.webappdemo
Define value for property 'artifactId': : MavenSpringHibernateMockitoDemo
Define value for property 'version': 1.0-SNAPSHOT:
Define value for property 'package': com.mayabansi.webappdemo:

(NOTE: I have left out the rest of standard maven output for brevity)

Above command created the project structure below:

rhasija@rhasija-desktop ~/workspace/blog_projects/MavenSpringHibernateMockitoDemo $ find . 
.
./src
./src/main
./src/main/webapp
./src/main/webapp/index.jsp
./src/main/webapp/WEB-INF
./src/main/webapp/WEB-INF/web.xml
./src/main/resources
./pom.xml

rhasija@rhasija-desktop ~/workspace/blog_projects/MavenSpringHibernateMockitoDemo $ 

Step #2: Add basic project to source code control.

Step #3: Make project ready for IntelliJ Idea/Eclipse.

Step #4: Add JBoss Remote Repository as alternate remote repository for Hibernate artifacts.

Step #5: Add Hibernate related dependencies.

Step #6: Add Enterprise Bundle repository to settings.xml

Step #7: Add Spring MVC related dependencies.

Step #8: Add Mockito dependency.

NOTE: The example is complete. I will keep adding to it as I learn new things so as to make the example project richer.

The entire project can be downloaded from GitHub or you can use Git to checkout the project:

git clone git@github.com:RaviH/MavenSpringHibernateMockito.git