Search code examples
javahibernatemapstruct

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


I upgraded my entity library to Hibernate 6.2.7 from version 5.6.15. Added some new DTOs and Mapstruct mappers. Suddenly I am getting following error:

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

My build.gradle file is as follows:

    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()
}

One of the Mapstruct mapper looks like this. It was autogenerated by JPA buddy plugin of IntelliJ Idea.

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);
}

Solution

  • Use componentModel = "spring" instead of componentModel = "cdi" in the @Mapper annotation of MapStruct. By specifying componentModel = "spring", MapStruct produces the mapper as a Spring bean, simplifying injection into other Spring components. Alternatively, you can include the necessary dependencies for CDI ("cdi") to achieve the desired functionality.