websphere-liberty:23.X 图像中的 @Customizer -> 无法转换为类 org.eclipse.persistence.config.DescriptorCustomizer

问题描述 投票:0回答:1

我们正在使用 websphere-liberty:23.0.0.11-kernel-java17-openj9-ubi

有功能

<feature>persistence-3.0</feature> 

我们想使用 EclipseLink History https://wiki.eclipse.org/EclipseLink/Examples/JPA/History

我们添加

@Customizer(HistoryCustomizer.class)

我们的实体类 但我们总是遇到代码异常 问题是 我们应该将哪个artifactId/版本添加到pom.xml以支持带有Liberty和持久性3.0的EclipseLink?

我添加了这样的依赖

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.core</artifactId>
        <version>3.0.4</version>
        <scope>compile</scope>
    </dependency>

和我的描述符定制器

import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.history.HistoryPolicy;

public class HistoryCustomizer implements DescriptorCustomizer {

    public void customize(ClassDescriptor descriptor) {
        HistoryPolicy policy = new HistoryPolicy();
        policy.addHistoryTableName("EMPLOYEE_HIST");
        policy.addStartFieldName("START_DATE");
        policy.addEndFieldName("END_DATE");
        descriptor.setHistoryPolicy(policy);
    }
}

,但我收到了有关 ClassCastException 的错误消息

jakarta.persistence.PersistenceException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 3.0.3.v202208190922): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [jpa-unit] failed. Close all factories for this PersistenceUnit.
Internal Exception: java.lang.ClassCastException: class com.ibm.saas.siusermgr.application.scope.HistoryCustomizer cannot be cast to class org.eclipse.persistence.config.DescriptorCustomizer (com.ibm.saas.siusermgr.application.scope.HistoryCustomizer is in unnamed module of loader com.ibm.ws.classloading.internal.AppClassLoader @2bba175e; org.eclipse.persistence.config.DescriptorCustomizer is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @3cab2487)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:907)
websphere eclipselink websphere-liberty
1个回答
0
投票

配置对第三方 API 的访问

doc中所述:

默认情况下,应用程序无权访问 Open Liberty 中提供的第三方 API

因此,即使

persistence-3.0
功能文档提供像org.eclipse.persistence.config.DescriptorCustomizer
这样的API
(加上
HistoryPolicy
和其他EclipseLink类),如果没有额外的服务器配置,这些将无法供您的应用程序使用。

服务器.xml

为您的应用程序配置一个

<classloader>
元素,例如:

<application id="testApp" name="testApp" type="war" location="testApp.war">
    <classloader apiTypeVisibility="+third-party" />
</application>

建议“提供”范围依赖

使用

provided
范围依赖项从运行时提供的库版本中明确加载这些类,而不是将其打包在应用程序中,可能会减少混淆的机会。确实,默认的“父级优先”类加载一开始可能会让这并不重要。不过,我建议这是一个更好的起点,除非/直到您确实需要考虑使用您自己的版本(超出了本文的范围)。它的占地面积也较小。

pom.xml

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.core</artifactId>
        <version>3.0.4</version>
        <scope>provided</scope>
    </dependency>

参考文献

© www.soinside.com 2019 - 2024. All rights reserved.