Hibernate version mismatch in WildFly 10.0

2019-08-01 20:24发布

问题:

I have 2 projects, one is a normal java project (with maven): core, and the other is non-maven dynamic web project: webapi. The later acts as a rest layer on top of core.

core uses hibernate without any problem. It defines the persistence.xml (inside src/META-INF/) as follows:

<?xml version="1.0" encoding="utf-8"?>

<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="app" transaction-type="RESOURCE_LOCAL">

        <class>data.entities.Anything</class>
        <class>data.entities.Something</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
            <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost;databaseName=AppDb"></property>
            <property name="javax.persistence.jdbc.user" value="****"></property>
            <property name="javax.persistence.jdbc.password" value="****"></property>
            <property name="hibernate.default_schema" value="dbo"></property>           
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"></property>
            <property name="hibernate.hbm2ddl.auto" value="update"></property>
        </properties>
    </persistence-unit>
</persistence>

webapi uses core and try to expose core's functionality as service. I have added core to both "Java Build Path", and "Deployment Assembly" of webapi. As I am using SQL Server, sqljdbc42.jar is also added to "Deployment Assembly" of webapi.

However, when I try to deploy webapi in WildFly 10.0, the hibernate entity manager is not instantiated properly. My primary suspect here the version mismatch of hibernate-core in core, and webapi. core uses hibernate-core 5.2.4.Final, whereas for webapi it is coming from WildFly runtime, which is 5.0.7.Final. This can be seen from the logs below:

  • core log:

    [HibernateManager.:19] - Trying to create entityManager for persistence-unit: app

    [LogHelper.logPersistenceUnitInformation:31] - HHH000204: Processing PersistenceUnitInfo [ name: app ...]

    [Version.logVersion:45] - HHH000412: Hibernate Core {5.2.4.Final}

    [Environment.:213] - HHH000206: hibernate.properties not found

    [Environment.buildBytecodeProvider:318] - HHH000021: Bytecode provider name : javassist

    [JavaReflectionManager.:66] - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

    [DriverManagerConnectionProviderImpl.configure:70] - HHH10001002: Using Hibernate built-in connection pool (not for production use!)

    [DriverManagerConnectionProviderImpl.buildCreator:126] - HHH10001005: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost;databaseName=AppDb]

    [DriverManagerConnectionProviderImpl.buildCreator:135] - HHH10001001: Connection properties: {user=****, password=****}

    [DriverManagerConnectionProviderImpl.buildCreator:140] - HHH10001003: Autocommit mode: false

    [PooledConnections.:41] - HHH000115: Hibernate connection pool size: 20 (min=1)

    [Dialect.:153] - HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect

    [Version.:30] - HV000001: Hibernate Validator 5.2.3.Final

    [DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection:47] - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@19ccca5] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.

    [HibernateManager.:22] - Created entityManager for persistence-unit: app

    [QueryTranslatorFactoryInitiator.initiateService:47] - HHH000397: Using ASTQueryTranslatorFactory

  • webapi log:

    [org.jboss.as.jpa] (MSC service thread 1-2) WFLYJPA0002: Read persistence.xml for app

    [org.jboss.as.jpa] (ServerService Thread Pool -- 29) WFLYJPA0010: Starting Persistence Unit (phase 1 of 2) Service 'webapi.war#app'

    [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 29) HHH000204: Processing PersistenceUnitInfo [ name: app ...]

    [org.hibernate.Version] (ServerService Thread Pool -- 29) HHH000412: Hibernate Core {5.0.7.Final}

    [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 29) HHH000206: hibernate.properties not found

    [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 29) HHH000021: Bytecode provider name : javassist

    [org.hibernate.orm.deprecation] (ServerService Thread Pool -- 29) HHH90000001: Found usage of deprecated setting for specifying Scanner [hibernate.ejb.resource_scanner]; use [hibernate.archive.scanner] instead

    [org.hibernate.annotations.common.Version] (ServerService Thread Pool -- 29) HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

    [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) WFLYJCA0004: Deploying JDBC-compliant driver class com.microsoft.sqlserver.jdbc.SQLServerDriver (version 6.0)

    [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) WFLYJCA0018: Started Driver service with driver-name = webapi.war_com.microsoft.sqlserver.jdbc.SQLServerDriver_6_0

    [org.jboss.as.jpa] (ServerService Thread Pool -- 29) WFLYJPA0010: Starting Persistence Unit (phase 2 of 2) Service 'webapi.war#app'

    [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 29) HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect

    [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 29) Envers integration enabled? : true

    [org.hibernate.validator.internal.util.Version] (ServerService Thread Pool -- 29) HV000001: Hibernate Validator 5.2.3.Final

    [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 29) HHH000228: Running hbm2ddl schema update

    [org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl] (ServerService Thread Pool -- 29) HHH000262: Table not found: Anythings

    [org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl] (ServerService Thread Pool -- 29) HHH000262: Table not found: Somethings

    [org.wildfly.extension.undertow] (ServerService Thread Pool -- 28) WFLYUT0021: Registered web context: /webapi

    [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0010: Deployed "webapi.war" (runtime-name : "webapi.war")

How this can be rectified?

回答1:

This can be solved by changing the version of your Hibernate core in Wildfly. In order to do so, change your META-INF/persistence.xml file like this :

<properties>
    <property name="jboss.as.jpa.providerModule" value="org.hibernate:5.2" />
    ...
</properties>

To add some micro version:

<property name="jboss.as.jpa.providerModule" value="org.hibernate:5.2.4.Final" />

Courtesy link : http://in.relation.to/2016/07/07/updating-hibernate-orm-in-wildfly/