Mapstruct in Spring Boot Sets all fields to null

928 Views Asked by At

I have Spring Boot application (v3.0.2, Java 17), and in it, a simple entity ActivityType and corresponding ActivityDto.

 //Entity (uses Lombok 1.18.24)...
 
     @Getter
     @Setter
     @Entity
     public class ActivityType {
        @Id
        @Column(name = "ActivityTypeId", nullable = false)
        private Integer id;
     
        @Column(name = "ActivityName", nullable = false, length = 30)
        private String activityName;
     
        @Column(name = "ActivityDescription")
        private String activityDescription;
     
     }
 
 //DTO...
 
         public record ActivityTypeDto(
            Integer id, 
            String activityName, 
            String activityDescription) implements Serializable {
         }

I'm using IntelliJ Idea (v2022.2.4) and JPA Buddy (v2022.5.4-222) to generate the Mapper Interface (MapStruct v1.5.3.Final). When I build the Mapper implementation, in the generated code, both the toEntity and toDto methods are incorrect.

 @Component public class ActivityTypeMapperImpl implements ActivityTypeMapper {
 
     @Override
     public ActivityType toEntity(ActivityTypeDto activityTypeDto) {
         if ( activityTypeDto == null ) {
             return null;
         }
 
         ActivityType activityType = new ActivityType();
 
         return activityType;
     }
 
     @Override
     public ActivityTypeDto toDto(ActivityType activityType) {
         if ( activityType == null ) {
             return null;
         }
 
     // What's this all about?? Why not activityType.id, etc??
         Integer id = null;
         String activityName = null;
         String activityDescription = null;
 
         ActivityTypeDto activityTypeDto = new ActivityTypeDto( id, activityName, activityDescription );
 
         return activityTypeDto;
     }
 
     @Override
     public ActivityType partialUpdate(ActivityTypeDto activityTypeDto, ActivityType activityType) {
         if ( activityTypeDto == null ) {
             return activityType;
         }
 
         return activityType;
     } 

I've tried various alternatives, including using a class for the DTO instead of a record, but no success. Looks like I've missed something, but not sure what.


Update:

I can fix this by not using Lombok for the Entity getters/setters, which leads me on to final question, is there a setting on the MapStruct plugin to take Lomboz into account?

1

There are 1 best solutions below

2
On

please define you entity like this,

 @Entity
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
 public class ActivityType {
    @Id
    @Column(name = "ActivityTypeId", nullable = false)
    private Integer id;
 
    @Column(name = "ActivityName", nullable = false, length = 30)
    private String activityName;
 
    @Column(name = "ActivityDescription")
    private String activityDescription;
 
 }

then define ActivityTypeDTO like this,

@Data
public class ActivityTypeDTO {

   @JsonProperty("id")
   private Integer id;

   @JsonProperty("ActivityName")
   private String ActivityName;

   @JsonProperty("activityDescription")
   private String activityDescription;

best practice to use MapStruct is like this,

@Mapper(componentModel = "spring", uses = {})
public interface ActivityMapper extends EntityMapper<ActivityTypeDTO, ActivityType> {

   ActivityTypeDTO toDto(ActivityType activityType);
   ActivityType toEntity(ActivityTypeDTO activityTypeDTO);
}

and EntityMApper in Mapper should be like this,

public interface EntityMapper<D, E> {
   E toEntity(D dto);
   D toDto(E entity);
}

Now I am sure you mapper work correctly.