Search code examples
javahibernatejpajbosswildfly

Hibernate JPA ClassCastException when moving application from JBoss 7 to WildFly 8


I have a somewhat crufty piece of software that is currently running under Java 7 and JBoss 7.1.2. As a first step to bringing this up to current times, I'm trying to bump this up to running under Java 8 and WildFly 8.2.1.

I've managed to get most things working, but am running into an issue with Hibernate / JPA versions. My application is packaged as an EAR file with two WAR files, along with a few JAR files. One of the WAR files is using Hibernate 4.3.x and JPA 2.1 and appears to be working ok, but the other WAR file (mostly 3rd party code) is using Hibernate 3.6.x and JPA 2.0.

Somehow someone managed to magically make that all work under JBoss 7.1.x, but I've bene unable to repeat that magic under WildFly 8.2.x and keep getting exceptions such as the following:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.webflow.execution.FlowExecutionException: Exception thrown in state 'ipaidLoginForm' of flow 'login'
[...]
Caused by: java.lang.ClassCastException: org.hibernate.jpa.HibernatePersistenceProvider cannot be cast to javax.persistence.spi.PersistenceProvider
    at javax.persistence.Persistence$1.isLoaded(Persistence.java:92)
    at org.hibernate.validator.internal.engine.resolver.JPATraversableResolver.isReachable(JPATraversableResolver.java:56)
    at org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver.isReachable(DefaultTraversableResolver.java:137)
    at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation.isReachable(CachingTraversableResolverForSingleValidation.java:46)
    at org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1396)
    ... 75 more

I have been trying just about every combination I can think of to include / exclude versions of Hibernate and JPA. I believe the solution should be the exclude the WildFly built-in Hibernate and JPA and use the two versions that are bundled in my two WARs.

My jboss-deployment-structure.xml file looks like this:

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
    <ear-subdeployments-isolated>false</ear-subdeployments-isolated>
    <deployment>
        <exclusions>
            <module name="org.hibernate" />
            <module name="javax.persistence.api" />
            <module name="org.jboss.resteasy.resteasy-jackson-provider" />
        </exclusions>
        <dependencies>
            <module name="javax.servlet.jstl.api" export="true" />
            <module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import" />
            <module name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>

The WAR file that is not working has the following persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">

    <persistence-unit name="CasPersistence" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>org.jasig.cas.services.AbstractRegisteredService</class>
        <class>org.jasig.cas.services.RegexRegisteredService</class>
        <class>org.jasig.cas.services.RegisteredServiceImpl</class>
        <class>org.jasig.cas.ticket.TicketGrantingTicketImpl</class>
        <class>org.jasig.cas.ticket.ServiceTicketImpl</class>
        <class>org.jasig.cas.ticket.registry.support.JpaLockingStrategy$Lock</class>
        <!-- DriveSync specific change - Add proper for hibernate cache -->
        <properties>
            <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
        </properties>
    </persistence-unit>
</persistence>

Somehow, the WAR file that is working doesn't seem to have a persistence.xml at all? I'm not sure if that is normal?

Any suggestions, even just pointing in the right direction, would be hugely appreciated!


Solution

  • I think the issue is that you haven't isolated your subdeployments thus you are getting the 2 hibernate and jpa versions to collide. https://www.mastertheboss.com/jbossas/jboss-as-7/jboss-as-7-classloading/ might help in your case