0

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?

Vicki
  • 668
  • 7
  • 21
  • It appears that this questions is answered [here](https://stackoverflow.com/q/47676369/1346076) – Vicki Feb 10 '23 at 11:01
  • 2
    It looks like you haven't specified Lombok annotation processor in your build file. Please refer to the [Lombok documentation](https://projectlombok.org/setup/) to install it. The [link](https://stackoverflow.com/a/47684351/3136181) from the previous comment will help you to configure everything properly. – Andrey Belyaev Feb 13 '23 at 07:29
  • Hi Andrey, this was indeed the case and the problem has now been resolved. Thanks for your comment – Vicki Feb 14 '23 at 12:19

1 Answers1

0

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.

amir-rad
  • 85
  • 7
  • Thanks for you comment Amir. The problem was resolved using the response here https://stackoverflow.com/q/47676369/1346076 – Vicki Feb 14 '23 at 12:22
  • happy about you,I am sure what I answered work based on your question, pls be careful about what you ask, good question and problem description have half of correct response. – amir-rad Feb 19 '23 at 09:34