hibernate 如何修复错误:找不到任何CDI或Jakarta CDI依赖项类型,您是否丢失了类路径的依赖项?

1cklez4t  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(192)

我将我的实体库从5.6.15版本升级到Hibernate 6.2.7。添加了一些新的DTO和MapstructMap器。突然出现以下错误:

error: Couldn't find any of the CDI or Jakarta CDI Dependency types. Are you missing a dependency on your classpath?

我的build.gradle文件如下:

plugins {
    id 'java'
}

group 'com.iiot-wizards'
version '3.0.0.0'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
//
//    // https://mvnrepository.com/artifact/org.hibernate/hibernate-core
//    implementation group: 'org.hibernate', name: 'hibernate-core', version: '5.6.15.Final'

    // https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core
    implementation group: 'org.hibernate.orm', name: 'hibernate-core', version: '6.2.7.Final'

    // https://mvnrepository.com/artifact/org.projectlombok/lombok
    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.28'
    annotationProcessor 'org.projectlombok:lombok:1.18.28'

    // https://mvnrepository.com/artifact/org.mapstruct/mapstruct
    implementation group: 'org.mapstruct', name: 'mapstruct', version: '1.5.5.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'

    // https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding
    implementation 'org.projectlombok:lombok-mapstruct-binding:0.2.0'

    // https://mvnrepository.com/artifact/org.mapstruct/mapstruct
    implementation 'org.mapstruct:mapstruct:1.5.5.Final'

    // https://mvnrepository.com/artifact/org.springframework/spring-context
    implementation 'org.springframework:spring-context:6.0.11'

    testCompileOnly "org.projectlombok:lombok:1.18.26"
    testAnnotationProcessor "org.projectlombok:lombok:1.18.26"
}

jar {
    manifest {
        attributes(
                'Class-Path': 'antlr-2.7.7.jar byte-buddy-1.10.7.jar classmate-1.5.1.jar dom4j-2.1.1.jar FastInfoset-1.2.15.jar hibernate-commons-annotations-5.1.0.Final.jar hibernate-core-5.4.11.Final.jar istack-commons-runtime-3.0.7.jar jandex-2.1.1.Final.jar javassist-3.24.0-GA.jar javax.activation-api-1.2.0.jar javax.persistence-api-2.2.jar jaxb-api-2.3.1.jar jaxb-runtime-2.3.1.jar jboss-logging-3.3.2.Final.jar jboss-transaction-api_1.2_spec-1.1.1.Final.jar stax-ex-1.8.jar txw2-2.3.1.jar'
        )
    }
}

test {
    useJUnitPlatform()
}

其中一个MapstructMap器如下所示。它是由IntelliJ Idea的JPA好友插件自动生成的。

package com.iiotwizards.model.mappers;

import com.iiotwizards.model.UserManagement.UserAdminUsersEntity;
import com.iiotwizards.model.dtos.UserAdminUsersEntityDto;
import com.iiotwizards.model.UserManagement.UserTokenEntity;
import org.mapstruct.*;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, componentModel = MappingConstants.ComponentModel.CDI)
public interface UserAdminUsersEntityMapper
{
    UserAdminUsersEntity toEntity(UserAdminUsersEntityDto userAdminUsersEntityDto);

    @AfterMapping
    default void linkEmailSettings(@MappingTarget UserAdminUsersEntity userAdminUsersEntity)
    {
        userAdminUsersEntity.getEmailSettings().forEach(emailSetting -> emailSetting.setDefaultSender(userAdminUsersEntity));
    }

    @AfterMapping
    default void linkOpcAeConditions(@MappingTarget UserAdminUsersEntity userAdminUsersEntity)
    {
        userAdminUsersEntity.getOpcAeConditions().forEach(opcAeCondition -> opcAeCondition.setUser(userAdminUsersEntity));
    }

    @AfterMapping
    default void linkOpcAeEmailRecipients(@MappingTarget UserAdminUsersEntity userAdminUsersEntity)
    {
        userAdminUsersEntity.getOpcAeEmailRecipients().forEach(opcAeEmailRecipient -> opcAeEmailRecipient.setUser(userAdminUsersEntity));
    }

    @AfterMapping
    default void linkUserAdminPasswordResetTokens(@MappingTarget UserAdminUsersEntity userAdminUsersEntity)
    {
        userAdminUsersEntity.getUserAdminPasswordResetTokens().forEach(userAdminPasswordResetToken -> userAdminPasswordResetToken.setUserId(userAdminUsersEntity.getId()));
    }

    @AfterMapping
    default void linkUserAdminVerificationTokens(@MappingTarget UserAdminUsersEntity userAdminUsersEntity)
    {
        userAdminUsersEntity.getUserAdminVerificationTokens().forEach(userAdminVerificationToken -> userAdminVerificationToken.setUserId(userAdminUsersEntity.getId()));
    }

    @AfterMapping
    default void linkUserEmails(@MappingTarget UserAdminUsersEntity userAdminUsersEntity)
    {
        userAdminUsersEntity.getUserEmails().forEach(userEmail -> userEmail.setUser(userAdminUsersEntity));
    }

    @AfterMapping
    default void linkUserImages(@MappingTarget UserAdminUsersEntity userAdminUsersEntity)
    {
        userAdminUsersEntity.getUserImages().forEach(userImage -> userImage.setUser(userAdminUsersEntity));
    }

    @AfterMapping
    default void linkWebUiUserToken(@MappingTarget UserAdminUsersEntity userAdminUsersEntity)
    {
        UserTokenEntity webUiUserToken = userAdminUsersEntity.getWebUiUserToken();
        if (webUiUserToken != null)
        {
            webUiUserToken.setUserAdminUsersEntity(userAdminUsersEntity);
        }
    }

    UserAdminUsersEntityDto toDto(UserAdminUsersEntity userAdminUsersEntity);

    @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    UserAdminUsersEntity partialUpdate(UserAdminUsersEntityDto userAdminUsersEntityDto, @MappingTarget UserAdminUsersEntity userAdminUsersEntity);
}
e3bfsja2

e3bfsja21#

您遇到的错误消息“找不到任何CDI或Jakarta CDI依赖项类型”表明您的项目中存在与CDI(上下文和依赖项注入)相关的冲突或缺少配置。
从Hibernate 6开始,CDI是Hibernate核心框架的必需部分。如果您遇到此错误,则可能表示您的安装程序存在兼容性问题,例如:
1.依赖性冲突:类路径中可能存在冲突版本的CDI或相关库。
1.缺少依赖项:您的项目可能缺少适当的CDI或Jakarta CDI依赖项。
要解决此问题,您可以执行以下步骤:

  1. Gradle提供了一个名为“Dependency Insights”的有用插件,可以帮助您分析和可视化依赖关系冲突。要使用此插件,请将以下内容添加到build.gradle
plugins {
    id "com.gradle.plugin-publish" version "0.16.1" // Required for some plugins to work
    id "com.github.ben-manes.versions" version "0.36.0"
}

应用插件后,您可以运行以下命令:

./gradlew dependencyInsight --dependency conflicting-module

1.如果你需要强制依赖的特定版本来解决冲突,你可以在build.gradle文件中使用resolutionStrategy,如下所示:

configurations.all {
    resolutionStrategy.force 'com.example:conflicting-module:desired-version'
}

1.你也可以考虑在你的build.gradle中排除一个特定的传递依赖,如下所示:

implementation (group: 'org.hibernate.orm', name: 'hibernate-core', version: '6.2.7.Final') {
    exclude group: 'javax.enterprise', module: 'cdi-api'
}

这应该最有希望解决你的问题。请让我知道这个问题是否仍然存在。

相关问题