You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mapstruct映射报错:不存在orderIdentification.identification属性问题

解决MapStruct实体与DTO映射报错问题

问题本质

报错提示Type "ABCEntity" has no properties,核心原因是MapStruct无法识别实体类的属性,通常是Lombok注解处理器与MapStruct协同失效,或是嵌套属性映射写法的兼容性问题。

具体解决方案

1. 配置Lombok与MapStruct的兼容依赖

MapStruct和Lombok需要通过专用绑定包协同工作,否则MapStruct生成代码时无法识别Lombok自动生成的Getter方法。

Maven依赖配置示例

<dependencies>
    <!-- MapStruct核心依赖 -->
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.5.5.Final</version>
    </dependency>
    <!-- Lombok依赖 -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.28</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source> <!-- 匹配你的JDK版本 -->
                <target>11</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.5.5.Final</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.28</version>
                    </path>
                    <!-- 关键绑定包,让两者协同工作 -->
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>0.2.0</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

Gradle依赖配置示例

plugins {
    id 'java'
    id 'io.freefair.lombok' version '6.6.3'
}

dependencies {
    implementation 'org.mapstruct:mapstruct:1.5.5.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
    annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0'
}

2. 改用表达式处理嵌套属性映射

如果依赖配置后仍有问题,可以直接通过expression手动指定属性获取逻辑,同时处理空值避免空指针:

@Mapper
public interface ABCMapper {
    @Mapping(target = "orderIdentification", expression = "java(abcEntity.getOrderIdentification() != null ? abcEntity.getOrderIdentification().getIdentification() : null)")
    ABCDto toABC(ABCEntity abcEntity);
}

3. 检查IDE注解处理器配置

  • IntelliJ IDEA:安装Lombok插件后,进入Settings > Build, Execution, Deployment > Compiler > Annotation Processors,勾选Enable annotation processing。
  • 执行清理编译命令:Maven运行mvn clean compile,Gradle运行./gradlew clean build,清除缓存后重新生成映射代码。

4. 临时验证方案:手动添加Getter方法

如果以上方法都无效,可以暂时移除Lombok的@Getter注解,手动给ABCEntity和OrderEntity编写Getter方法,验证MapStruct是否能正常识别属性:

public class ABCEntity {
    @Id
    private String identification;
    private OrderEntity orderIdentification;

    public String getIdentification() {
        return identification;
    }

    public OrderEntity getOrderIdentification() {
        return orderIdentification;
    }
}

内容的提问来源于stack exchange,提问作者emoleumassi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 15:24:15