Mapstruct 1.2.0.Final版本嵌入式类型转换失败求助
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
Jobclass should be annotated with@Embeddable - In
JobData, theJobfield needs@Embedded(you can use@AttributeOverridesif 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 toJobData.job.idandJobData.job.title, add explicit@Mappingannotations:@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
Jobfield, 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/annotationsfor 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.xmlshould 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
mapstructandmapstruct-processorare 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
mapToJobDatamethod: if there's no code settingjobData.setJob(...), MapStruct didn't recognize the mapping. This points back to missing@Mappingannotations or incorrect field names.
Final Quick Check
Make sure you're using the mapper correctly in your test:
- Use the
INSTANCEfield from your mapper interface (e.g.,JobDataMapper.INSTANCE.mapToJobData(source)) - Avoid creating a new instance of the mapper directly—MapStruct handles instantiation via the
Mappersfactory.
Give these steps a try, and you should be able to track down why that job field is null!
内容的提问来源于stack exchange,提问作者StillLearningToCode

