如何在 Wildfly 21 上配置 Jakarta 持久性

Posted

技术标签:

【中文标题】如何在 Wildfly 21 上配置 Jakarta 持久性【英文标题】:How to configure Jakarta persistence on wildfly 21 【发布时间】:2021-05-22 17:09:07 【问题描述】:

在此先感谢您,并表示歉意!

我正在努力将 Wildfly 版本从 8 升级到 21。 早在 8 世纪的时候,wildfly 为默认的持久性引擎提供了 hibernate,现在它似乎与 Jakarta 一起提供了。因此,我正在经历尝试使持久性正常工作的过程,并遇到了下面列出的错误。

我有一个 21.0.2.Final 的干净下载,我修改了standalone.xml 以包含我认为正确加载的数据源,因为这些行最终会出现在日志中。

10:48:46,578 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-3) WFLYJCA0001: Bound data source [java:jboss/datasources/unitTestDS]
10:48:46,578 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-6) WFLYJCA0001: Bound data source [java:jboss/datasources/localLiveDS]

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="ertDatasource">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:jboss/datasources/localLiveDS</jta-data-source>
        <properties>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

test-persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
    <persistence-unit name="ertDatasource">
        <jta-data-source>java:jboss/datasources/unitTestDS</jta-data-source>
        <properties>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
        </properties>
    </persistence-unit>
</persistence>

这是我在运行 ./stabdalone.sh 命令时遇到的错误和警告

10:48:41,634 WARN  [org.jboss.as.domain.management.security] (MSC service thread 1-4) WFLYDM0111: Keystore /Applications/Wildfly/wildfly-21.0.2.Final-Clean/standalone/configuration/application.keystore not found, it will be auto generated on first use with a self signed certificate for host localhost

10:48:46,749 WARN  [org.jboss.modules.define] (MSC service thread 1-5) Failed to define class org.hibernate.ejb.HibernatePersistence in Module "deployment.<project name>-api.war" from Service Module Loader: java.lang.IncompatibleClassChangeError: Failed to link org/hibernate/ejb/HibernatePersistence (Module "deployment.<project name>-api.war" from Service Module Loader): class org.hibernate.ejb.HibernatePersistence has interface org.hibernate.ejb.AvailableSettings as super class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:839)
    at org.jboss.modules.ModuleClassLoader.doDefineOrLoadClass(ModuleClassLoader.java:424)
    at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:555)
    at org.jboss.modules.ModuleClassLoader.loadClassLocal(ModuleClassLoader.java:339)
    at org.jboss.modules.ModuleClassLoader$1.loadClassLocal(ModuleClassLoader.java:126)
    at org.jboss.modules.Module.loadModuleClass(Module.java:731)
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:247)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:410)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
    at org.jboss.as.jpa.processor.PersistenceProviderHandler.deploy(PersistenceProviderHandler.java:73)
    at org.jboss.as.jpa.processor.PersistenceBeginInstallProcessor.deploy(PersistenceBeginInstallProcessor.java:49)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:182)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1739)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1701)
    at org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1559)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.lang.Thread.run(Thread.java:748)

10:48:46,751 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-5) MSC000001: Failed to start service jboss.deployment.unit."<project name>-api.war".FIRST_MODULE_USE: org.jboss.msc.service.StartException in service jboss.deployment.unit."<project name>-api.war".FIRST_MODULE_USE: WFLYSRV0153: Failed to process phase FIRST_MODULE_USE of deployment "<project name>-api.war"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:189)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1739)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1701)
    at org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1559)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IncompatibleClassChangeError: Failed to link org/hibernate/ejb/HibernatePersistence (Module "deployment.<project name>-api.war" from Service Module Loader): class org.hibernate.ejb.HibernatePersistence has interface org.hibernate.ejb.AvailableSettings as super class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:839)
    at org.jboss.modules.ModuleClassLoader.doDefineOrLoadClass(ModuleClassLoader.java:424)
    at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:555)
    at org.jboss.modules.ModuleClassLoader.loadClassLocal(ModuleClassLoader.java:339)
    at org.jboss.modules.ModuleClassLoader$1.loadClassLocal(ModuleClassLoader.java:126)
    at org.jboss.modules.Module.loadModuleClass(Module.java:731)
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:247)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:410)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
    at org.jboss.as.jpa.processor.PersistenceProviderHandler.deploy(PersistenceProviderHandler.java:73)
    at org.jboss.as.jpa.processor.PersistenceBeginInstallProcessor.deploy(PersistenceBeginInstallProcessor.java:49)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:182)
    ... 8 more

10:48:46,815 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "<project name>-api.war")]) - failure description: "WFLYCTL0080: Failed services" => "jboss.deployment.unit.\"<project name>-api.war\".FIRST_MODULE_USE" => "WFLYSRV0153: Failed to process phase FIRST_MODULE_USE of deployment \"<project name>-api.war\"
    Caused by: java.lang.IncompatibleClassChangeError: Failed to link org/hibernate/ejb/HibernatePersistence (Module \"deployment.<project name>-api.war\" from Service Module Loader): class org.hibernate.ejb.HibernatePersistence has interface org.hibernate.ejb.AvailableSettings as super class"

10:48:46,861 ERROR [org.jboss.as] (Controller Boot Thread) WFLYSRV0026: WildFly Full 21.0.2.Final (WildFly Core 13.0.3.Final) started (with errors) in 22664ms - Started 336 of 599 services (1 services failed or missing dependencies, 373 services are lazy, passive or on-demand)

在 wildfly-root/modules/system/layers/base/javax/persistence/api/main 文件夹中,我有 module.xml 和 jakarta.persistence-api-2.2.3.jar

module.xml 内容

<?xml version="1.0" encoding="UTF-8"?>
<module name="javax.persistence.api" xmlns="urn:jboss:module:1.7">
    <dependencies>
        <!-- PersistenceUnitInfo needs javax.sql.DataSource -->
        <module name="javax.api" export="true"/>
    </dependencies>

    <resources>
        <resource-root path="jakarta.persistence-api-2.2.3.jar"/>
    </resources>
</module>

我已添加 Jakarta 持久性依赖项

<!-- https://mvnrepository.com/artifact/jakarta.persistence/jakarta.persistence-api -->
<dependency>
    <groupId>jakarta.persistence</groupId>
    <artifactId>jakarta.persistence-api</artifactId>
    <version>2.2.3</version>
</dependency>

我也有这个,不知道是否需要。

<dependency>
    <groupId>org.hibernate.common</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>4.0.4.Final</version>
</dependency>

想知道我错过了什么愚蠢的东西?

编辑: 我已将此添加到依赖项管理部分:

<dependency>
   <groupId>org.wildfly.bom</groupId>
   <artifactId>wildfly-jakartaee8-with-tools</artifactId>
   <scope>import</scope>
   <type>pom</type>
   <version>21.0.2.Final</version>
</dependency>

我现在得到了这个新错误:

14:20:07,862 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 82) MSC000001: Failed to start service jboss.persistenceunit."eventhub-api.war#ertDatasource": org.jboss.msc.service.StartException in service jboss.persistenceunit."eventhub-api.war#ertDatasource": java.lang.AbstractMethodError
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:198)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:128)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:659)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:213)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.lang.Thread.run(Thread.java:748)
    at org.jboss.threads.JBossThread.run(JBossThread.java:513)
Caused by: java.lang.AbstractMethodError
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:276)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1250)
    at org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:170)
    ... 10 more

14:20:07,865 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: "WFLYCTL0080: Failed services" => "jboss.persistenceunit.\"eventhub-api.war#ertDatasource\"" => "java.lang.AbstractMethodError
    Caused by: java.lang.AbstractMethodError"

【问题讨论】:

您的应用程序中不应包含这些 API jar,因此它应该在提供的范围内。您的应用程序 jar 中是否也有休眠功能? @ehsavoie 我同意,并且在部署设置中我会。但我只是想让 wildfly 在启动时不抛出异常。 【参考方案1】:

WildFLy 在部署您的应用程序时抛出异常。此外,您不应链接 WildFly 提供的 API,除非您从 WildFly 中删除 JPA 支持。此外,WildFly 与 jakartaEE 8 兼容,因此您不应将您的部署链接到 WildFLy 提供的某些 API。 对于 JakartaEE 9,您应该使用 WildFly 预览版

【讨论】:

我们使用 'provided' 范围的原因是因为我们将它部署到一个文档容器。而且,“FROM”行将是“Wildfly 21”码头基地。因此,我们知道该文件已经存在。另外,我只是想找出它为什么对我出错,以及如何解决它。稍后,一旦应用程序实际启动,我将努力使其正常工作。 由于某种原因,您没有使用与运行时使用的相同的休眠版本进行编译。没有您的申请,不能多说。 如何找出运行时使用的是哪一个? 实现同步的简单方法是使用 WildFly 的 BOMS github.com/wildfly/boms 如果您使用 JPA,则不应使用 Hibernate 进行编译,也不应在应用程序库中使用。 我这样做了,请参阅上面的“编辑”,我现在遇到了一个新错误。

以上是关于如何在 Wildfly 21 上配置 Jakarta 持久性的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 ActiveMQ Artemis 在 Wildfly 24 服务上配置 jms-queue

Wildfly 10 不显示调试消息

如何在 WildFly 中配置 EJB WebServices(入耳式)

在 Wildfly 10 上运行 EJB 2.1

安全域的 Wildfly 刷新缓存

Wildfly 集群设置问题