How to use hibernate 3.2.5 in EAP 6

Solution Unverified - Updated -

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.x
  • Hibernate 3.2.5
  • Postgresql 9.3

Issue

  • When migrating an application from jboss-ews-2.0(tomcat7) to JBoss EAP 6.0.0, get the errors related to hibernate 3.2.5.GA.

ERROR :

19:11:23,189 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-30) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/portal-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Could not instantiate dialect class
    at ...
Caused by: org.hibernate.HibernateException: Could not instantiate dialect class
    at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:84) [hibernate-3.2.5.ga.jar:3.2.5.ga]
    at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:42) [hibernate-3.2.5.ga.jar:3.2.5.ga]
    at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426) [hibernate-3.2.5.ga.jar:3.2.5.ga]
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128) [hibernate-3.2.5.ga.jar:3.2.5.ga]
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009) [hibernate-3.2.5.ga.jar:3.2.5.ga]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292) [hibernate-3.2.5.ga.jar:3.2.5.ga]
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:814) [spring-orm.jar:2.5.6]
    at ...
    ... 23 more
Caused by: java.lang.ClassCastException: org.hibernate.dialect.PostgreSQLDialect cannot be cast to org.hibernate.dialect.Dialect
    at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:78) [hibernate-3.2.5.ga.jar:3.2.5.ga]
    ... 33 more
  • Do you have an article / example for an application that includes its own hibernate 4 jars in WEB-INF/lib. I assume it can be done with changes to jboss-deployment-structure.xml .

Resolution

  • Please Note: Hibernate 3.x is not supported with JBoss EAP 6.

JBoss EAP 6 is only fully supported when used with the Hibernate 4 version distributed with the platform. The text below creates an unsupported deployment:

Please find the attached an example jboss-hibernate3-example-src.zip including the source code, configs and jboss-hibernate3.war.zip is the compiled war file.

It uses hibernate 3.2.5 and has been tested in EAP 6.0.0 + postgresql 9.3. Please use it as a reference to modify your web app accordingly.
PS. The example counts on a test table 'employee' which is described in the file org/lab/domain/Employee.hbm.xml.

  1. Install postgres jdbc driver as a module for EAP 6.0.0

    • location: jboss-eap-6.0.0/modules/*
  2. Add postgres driver and datasource in standalone.xml for EAP 6.0.0

    • location: jboss-eap-6.0.0/standalone/*
    • see subsystem xmlns="urn:jboss:domain:datasources:1.1"
    • please define your own datasource according to your environment
  3. Add persistence.xml to your web app. To use hibernate 3, this is required.

    • location: src/main/resources/META-INF/persistence.xml
    • set jboss.as.jpa.providerModule to hibernate3-bundled
    • set <jta-data-source>xxx</jta-data-source> . Here 'xxx' is the datasource defined in step 2
  4. Make sure the minimal required jars are in /WEB-INF/lib

    • location: src/main/webapp/WEB-INF/lib/*
    • these jars are shipped with hibernate 3.2.5 distribution which have no compatible issues with hibernate 3.2.5
  5. Modify jboss-deployment-structure.xml

    • location: src/main/webapp/WEB-INF/jboss-deployment-structure.xml
    • exclude the default modules shipped with EAP 6.0.0 (hibernate 4 and asm 3.3 which are not compatible with hibernate 3.2.5)
    • add the minimal dependencies for hibernate 3
  6. Modify hibernate.cfg.xml if there is any.

    • location: src/main/java/hibernate.cfg.xml
    • use the datasource defined in step 2.

Reference links:
[1] Add the PostgreSQL Module to the JBoss server
http://www.jboss.org/quickstarts/eap/index.html#add-the-postgresql-module-to-the-jboss-server
[2] Add the PostgreSQL Driver Configuration to the JBoss server
http://www.jboss.org/quickstarts/eap/index.html#add-the-postgresql-driver-configuration-to-the-jboss-server
[3] Packaging the Hibernate 3.5 or greater 3.x JPA persistence provider with your application
https://docs.jboss.org/author/display/AS7/JPA+Reference+Guide#JPAReferenceGuide-PackagingtheHibernate3.5orgreater3.xJPApersistenceproviderwithyourapplication

Root Cause

This error is caused by incompatible set of classes due to the fact that both Hibernate 4 and Hibernate 3 jars were included.
By default, EAP 6 is shipped with hibernate 4 as system module.

Attachments

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments