JBoss 使用 Hibernate 将 as7 迁移到 WildFly

JBoss as7 to WildFly migration with Hibernate

我尝试从 JBoss AS 7 迁移到 Wildfly 8.2。我真的有休眠问题。

我正在使用具有依赖项的 Maven:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.2.0.Final</version>
</dependency>

<dependency>
  <groupId>org.hibernate.javax.persistence</groupId>
  <artifactId>hibernate-jpa-2.0-api</artifactId>
  <version>1.0.1.Final</version>
</dependency>

当我启动我的应用程序时 在 AS 7 中工作我在 wildfly 中收到错误:

10:08:11,485 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 53) MSC000001: Failed to start service jboss.persistenceunit."com.haslerrail.MyDatabase.web.war#MyDatabase": org.jboss.msc.service.StartException in service jboss.persistenceunit."com.haslerrail.MyDatabase.web.war#MyDatabase": javax.persistence.PersistenceException: [PersistenceUnit: MyDatabase] Unable to build Hibernate SessionFactory
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.run(PersistenceUnitServiceImpl.java:172) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.run(PersistenceUnitServiceImpl.java:117) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
      at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.7.0_65]
      at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:474)
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.run(PersistenceUnitServiceImpl.java:182) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_65]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_65]
      at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_65]
      at org.jboss.threads.JBossThread.run(JBossThread.java:122)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: MyDatabase] Unable to build Hibernate SessionFactory
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1239) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access0(EntityManagerFactoryBuilderImpl.java:120) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:855) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:845) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
      at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44) [jipijapa-hibernate4-3-1.0.1.Final.jar:]
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.run(PersistenceUnitServiceImpl.java:154) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
      ... 8 more
Caused by: org.hibernate.AnnotationException: Attribute [com.haslerrail.MyDatabase.server.db.entity.Widget.type] was annotated as enumerated, but its java type is not an enum [java.lang.String]
      at org.hibernate.cfg.annotations.SimpleValueBinder.setType(SimpleValueBinder.java:257) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.cfg.annotations.PropertyBinder.makePropertyAndValue(PropertyBinder.java:195) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:216) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2241) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:852) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
      ... 13 more

我试图找到有关 hibernate 和 wildfly 的任何文档,但 https://docs.jboss.org/author/display/WFLY8/Hibernate 上的文档是空的!!!!

有没有人有从 AS 7 迁移到 Wildfly 和休眠的经验?我需要调整什么?

在 Java EE 容器中,Hibernate 的容器版本优先于您的应用程序打包版本。因此错误。

为避免这种情况,您需要显式控制类加载并排除容器库。阅读 here

基本上,您需要提供 jboss-deployment-structure.xml 以对依赖项进行精细控制。

例如:

    <jboss-deployment-structure>
        <deployment>
            <exclusions>
                <!-- Exclude container version of hibernate. By default its version 4, we want 
                    bundled version to load -->
                <module name="org.hibernate" slot="main" />
            </exclusions>
        </deployment>
    </jboss-deployment-structure>

经过几个小时的尝试,这个版本的 jboss-deployment-structure.xml 文件似乎对我有用:

<jboss-deployment-structure>
  <deployment>
    <exclusions>
      <module name="org.hibernate" slot="main" />
    </exclusions>
    <dependencies>
      <module name="org.hibernate" />
    </dependencies>
  </deployment>
</jboss-deployment-structure>

我想我还需要 "org.hibernate" 作为依赖项,因为我正在使用 Flyway 进行数据库迁移...否则我会收到一些错误消息。

没有Flyway,Rohit的答案似乎是正确的...(但我没有测试过!)