Why isn't my MapStruct mapper populating my entity objects correctly?
I believe I'm doing everything correctly, but for some reason, the mapping doesn't seem to work. Here are the details of my setup:
Mapper Interface:
@Mapper( componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, unmappedSourcePolicy = ReportingPolicy.IGNORE ) public interface UserTaskMapper { UserTask toEntity(UserTaskRequest userTaskRequest); UserTaskResponse toResponse(UserTask userTask); }Entity Class:
@Entity @Getter @Setter @RequiredArgsConstructor @Table(name = "user_tasks") public class UserTask { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne @JoinColumn(name = "user_id", nullable = false) private User user; @ManyToOne @JoinColumn(name = "task_id", nullable = false) private Task task; private Boolean isCompleted; private String projectUrl; private LocalDateTime submittedAt; }Request DTO:
@Getter @Setter public class UserTaskRequest { private Long taskId; private Set<Long> userIds; private Boolean isCompleted = false; private String projectUrl; }Response DTO:
@Getter @Setter public class UserTaskResponse { private Long id; private Long userId; private TaskResponse task; private Boolean isCompleted; private String projectUrl; private LocalDateTime submittedAt; }Generated Mapper Implementation:
@Generated( value = "org.mapstruct.ap.MappingProcessor", date = "2025-05-03T19:59:56+0400", comments = "version: 1.6.3, compiler: IncrementalProcessingEnvironment from gradle-language-java-8.13.jar, environment: Java 17.0.14 (Amazon.com Inc.)" ) @Component public class UserTaskMapperImpl implements UserTaskMapper { @Override public UserTask toEntity(UserTaskRequest userTaskRequest) { if (userTaskRequest == null) { return null; } UserTask userTask = new UserTask(); return userTask; } @Override public UserTaskResponse toResponse(UserTask userTask) { if (userTask == null) { return null; } UserTaskResponse userTaskResponse = new UserTaskResponse(); return userTaskResponse; } }
Here, the generated mapper is just returning empty objects without mapping any fields.
I've tried several fixes:
- Cleaning and rebuilding the project multiple times.
- Updating dependencies—I changed:
from versionimplementation 'org.mapstruct:mapstruct:1.6.3' annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.3'1.5.5.Final. But this didn't resolve the issue.
Despite these efforts, the mapping still isn't working correctly. How can I fix this?