【问题标题】:Wildfly 11 - update to Hibernate 5.2.x for "Native Hibernate use"Wildfly 11 - 更新到 Hibernate 5.2.x 以用于“本机 Hibernate 使用”
【发布时间】:2018-03-29 23:50:24
【问题描述】:

背景:在“Native Hibernate 使用”场景中使用 Spring + Hibernate 的 WAR 应用程序 (https://docs.jboss.org/author/display/WFLY/JPA+Reference+Guide#JPAReferenceGuide-NativeHibernateuse)

在使用与 WFLY 11 捆绑的 Hibernate 版本(即 5.1.10.Final)时,一切都按预期工作,并在 jboss-deployment-structure.xml 中为 org.hibernate 模块配置以下配置:

<jboss-deployment-structure>
    <deployment>
        <dependencies>
            ...
            <module name="org.hibernate"/>
            ...
        </dependencies>
        <exclude-subsystems>
            <subsystem name="jaxrs" />
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>

根据this guide,升级到 Hibernate 5.2.16.Final 时出现问题。据我了解,在模块目录中解压缩hibernate-orm-modules-5.2.16.Final-wildfly-11-dist.zip 后,我应该能够使用插槽 5.2 或 5.2.16.Final 定义 Hibernate 模块,即:

<jboss-deployment-structure>
    <deployment>
        <dependencies>
            ...
            <module name="org.hibernate" slot="5.2"/>
            ...
        </dependencies>
        <exclude-subsystems>
            <subsystem name="jaxrs" />
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>

但上面的配置导致

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1507)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:721)
    ... 32 more

&lt;module name="org.hibernate" slot="5.2"/&gt; 更改为例如&lt;module name="org.hibernate" slot="5.23456789"/&gt; Wildfly 引发了这样的模块版本不存在的异常,因此我假设模块已正确加载。

我在这个配置中遗漏了什么吗?添加persistence.xml&lt;property name="jboss.as.jpa.providerModule" value="org.hibernate:5.2"/&gt; 不适用于这种情况,因为我不使用由Wildfly 管理并注入@PersistenceContext 注释的持久性单元。

【问题讨论】:

    标签: wildfly


    【解决方案1】:

    我终于找到了解决问题的方法。

    我尝试将“Native Hibernate 使用”模式转换为 Wildfly 管理的实体管理器(这意味着引入 persistence.xml 和 Spring LocalContainerEntityManagerFactoryBean 负责解析 persistence.xml)。这引入了另一种特定于应用程序或配置的异常,在这种情况下无关紧要。

    真正有帮助的是记录由 WFLY 类加载器 ($JAVA_OPTS += '-verbose:class') 加载的 jar。事实证明,无论指定插槽(&lt;module name="org.hibernate" slot="5.2"/&gt; - 或者在 persistence.xml 的情况下:&lt;property name="jboss.as.jpa.providerModule" value="org.hibernate:5.2"/&gt;),WFLY 总是从主插槽加载一些核心类(例如 SessionFactory),这在最好的情况下会导致链接异常或最坏情况下的“静默”错误。

    用与${jboss.home.dir}/modules/system/layers/base/org/hibernate/5.2/module.xml 相同的内容替换主hibernate module.xml (${jboss.home.dir}/modules/system/layers/base/org/hibernate/main/module.xml) 有帮助,这意味着现在我的${jboss.home.dir}/modules/system/layers/base/org/hibernate/main/module.xml 看起来像这样:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <module-alias xmlns="urn:jboss:module:1.3"
        name="org.hibernate" slot="main"
        target-name="org.hibernate" target-slot="5.2.16.Final"
    />
    

    这样,Wildfly 会加载正确版本的 Hibernate jar,而不管“main”或“5.2”插槽如何,因为它们都指向相同的 Hibernate 版本。

    该解决方案适用于 persistence.xml 模式以及“Native Hibernate 使用”模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-11
      • 2018-08-10
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多