mavenspring-mvcjboss7.xhibernate3

Configure hibernate 3 in Jboss7


I'm trying to set up a Spring MVC + Hibernate WAR for deployment to Jboss 7.1.1 Final. My application using Hibernate 3.6.1 & So far my understanding is that Hibernate 4 is packaged with the AS and is the default persistence provider. I am not using persistent.xml file configuration.

I have followed all steps required to setup Hibernate 3 in JBoss.

  1. Go to the AS installation and change into the modules/org folder.
  2. Created folder for slot 3 to hold Hibernate 3
  3. Copied the Hibernate3 jars into this new AS/modules/org/hibernate/3 folder (hibernate3-core.jar, hibernate3-commons-annotations.jar, hibernate3-entitymanager.jar, dom4j.jar, slf4j.jar, slf4j-api.jar, commons-collections.jar, antlr.jar, slf4j-api.jar, commons-collections.jar, antlr.jar and any other jar needed for Hibernate 3)
  4. Created the AS/modules/org/hibernate/3/module.xml file with contents:

    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.0" name="org.hibernate" slot="3">
        <resources>
            <resource-root path="hibernate3-core.jar"/>
            <resource-root path="hibernate3-commons-annotations.jar"/>
            <resource-root path="hibernate3-entitymanager.jar"/>
            <resource-root path="javassist-3.12.0.GA.jar"/>
            <resource-root path="antlr-2.7.6.jar"/>  
            <resource-root path="commons-collections-3.1.jar"/>  
            <resource-root path="dom4j-1.6.1.jar"/>  
            <!-- Insert other Hibernate 3 jars to be used here -->
        </resources>
    
        <dependencies>
            <module name="org.jboss.as.jpa.hibernate" slot="3"/>
            <module name="asm.asm"/>
            <module name="javax.api"/>
            <module name="javax.persistence.api"/>
            <module name="javax.transaction.api"/>
            <module name="javax.validation.api"/>
            <module name="org.apache.ant"/>
            <module name="org.infinispan"/>
            <module name="org.javassist"/>
            <module name="org.slf4j"/>
        </dependencies>
    </module>
    

My pom.xml(part):

    <!-- Hibernate dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.6.Final</version>
        <exclusions>
            <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
            </exclusion>
        </exclusions>
        <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.5.6-Final</version>
</dependency>
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>
</dependency>
 <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>3.0.0.ga</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate.common</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>4.0.5.Final</version>
    </dependency>

My hibernate-context.xml(Part)

  <jpa:repositories base-package="com.gea.dvr.repository" />
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                 p:dataSource-ref="dataSource"
                 p:configLocation="${hibernate.config}"
                 p:packagesToScan="com.iana.dver"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
                p:driverClass="${app.jdbc.driverClassName}"
                p:jdbcUrl="${app.jdbc.url}"
                p:user="${app.jdbc.username}"
                p:password="${app.jdbc.password}"
                p:idleConnectionTestPeriod="30"
                p:maxPoolSize="100"
                p:maxStatements="50"
                p:minPoolSize="10" 
                p:maxIdleTime="60" />

    <!-- Declare a transaction manager-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
                p:sessionFactory-ref="sessionFactory" />                    

  <!-- Specify our ORM vendor -->
  <bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" 
              p:showSql="false"/>

My hibernate.cfg.xml

<hibernate-configuration>
  <session-factory name="sessionFactory">
    <!-- We're using MySQL database so the dialect needs to MySQL as well-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- Enable this to see the SQL statements in the logs-->
    <property name="show_sql">false</property>
    <!-- This will drop our existing database and re-create a new one.
            Existing data will be deleted! -->
    <!--  <property name="hbm2ddl.auto">update</property>-->

    <mapping class="com.gea.dvr.domain.DvrUserType"/>
    <mapping class="com.gea.dvr.domain.DvrUsers"/>
    <mapping class="com.gea.dvr.domain.DvrConfig"/>
    <mapping class="com.gea.dvr.domain.DvrDetail"/>
    <mapping class="com.gea.dvr.domain.DvrFiles"/>
    <mapping class="com.gea.dvr.domain.DvrNotif"/>
    <mapping class="com.gea.dvr.domain.UserLogin"/>
  </session-factory>
</hibernate-configuration>

I have provided all required java still I am still getting following exception, Can anyone help me to setup hibernate 3 related changes in JBoss?

11:03:14,597 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-3) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateVendor' defined in ServletContext resource [/WEB-INF/hibernate-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence

Solution

  • Since you get a java.lang.NoClassDefFoundError (and not a ClassNotFoundException), it's probably some sort of classloading issue.

    try adding hibernate-entitymanager dependency that will solve your problem.

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
            </exclusion>
            <exclusion>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>