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

Mapstruct 1.2.0.Final版本嵌入式类型转换失败求助

Troubleshooting MapStruct Embedded Type Null Issue

Hey there! Let's figure out why your embedded Job object is showing up as null in JobData after using MapStruct. Based on the error you shared (java.lang.AssertionError: Expected: is not null but: was null), here are the most common fixes and checks to run through:

1. Double-Check Your Embedded Class Annotations

First, make sure your JPA annotations are set up correctly for the embedded type:

  • Your Job class should be annotated with @Embeddable
  • In JobData, the Job field needs @Embedded (you can use @AttributeOverrides if customizing column names, but that's optional for basic mapping)
  • Example:
    @Embeddable
    public class Job {
        private Long id;
        private String title;
        // getters & setters
    }
    
    public class JobData {
        @Embedded
        private Job job;
        // other fields + getters & setters
    }
    

2. Verify MapStruct Mapper Configuration

MapStruct won't guess nested mappings automatically if source and target paths don't align perfectly. Here's what to check:

  • If your source object has flat fields (e.g., sourceJobId, sourceJobTitle) that need to map to JobData.job.id and JobData.job.title, add explicit @Mapping annotations:
    @Mapper
    public interface JobDataMapper {
        JobDataMapper INSTANCE = Mappers.getMapper(JobDataMapper.class);
    
        @Mapping(target = "job.id", source = "sourceJobId")
        @Mapping(target = "job.title", source = "sourceJobTitle")
        JobData mapToJobData(JobSource source);
    }
    
  • If your source object already has a Job field, ensure the field name matches exactly (case-sensitive!) in both source and target. If they don't match, use @Mapping(target = "job", source = "sourceJobField") to link them.

3. Ensure Annotation Processing is Enabled in IntelliJ 2017

Even with the MapStruct plugin installed, IntelliJ might not generate mapper implementation classes correctly. Fix this by:

  • Going to File > Settings > Build, Execution, Deployment > Compiler > Annotation Processors
  • Checking the box that says Enable annotation processing
  • Confirming the output directory for generated sources is set (usually target/generated-sources/annotations for Maven projects)
  • Rebuilding your project after changing this setting—this forces MapStruct to generate the mapper code.

4. Validate Your Dependencies

Make sure your build file (Maven/Gradle) includes the correct MapStruct dependencies, especially the annotation processor:

  • For Maven, your pom.xml should have:
    <dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.3.1.Final</version> <!-- Use a compatible version -->
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source> <!-- Match your Java version -->
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.3.1.Final</version> <!-- Same as above -->
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  • Mismatched versions between mapstruct and mapstruct-processor are a common cause of broken mappings.

5. Inspect the Generated Mapper Implementation

MapStruct generates concrete implementations of your mapper interface—check it to see if it's actually populating the job field:

  • In IntelliJ, navigate to target/generated-sources/annotations (for Maven)
  • Find the generated class (e.g., JobDataMapperImpl)
  • Check the mapToJobData method: if there's no code setting jobData.setJob(...), MapStruct didn't recognize the mapping. This points back to missing @Mapping annotations or incorrect field names.

Final Quick Check

Make sure you're using the mapper correctly in your test:

  • Use the INSTANCE field from your mapper interface (e.g., JobDataMapper.INSTANCE.mapToJobData(source))
  • Avoid creating a new instance of the mapper directly—MapStruct handles instantiation via the Mappers factory.

Give these steps a try, and you should be able to track down why that job field is null!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:02:09