Skip to content
Advertisement

Spring Boot Mapstruct not woking mapping

My Dto model

public class CoverLetterDto{
  
   private Long user_id;

}

My main model

public class CoverLetter {
      
       private User user;
    
    }

My mapstruct class

@Mapping(source="user_id", target = "user")
CoverLetter CoverLetterDtotoEntityCoverLetter(CoverLetterDto dto, User user);

Hi everyone my project using mapstruct but my dto class user_id not mapping user please help me Thanks

Advertisement

Answer

This should solve the problem:

@Mapper
public interface CoverLetterMapper {

    CoverLetterMapper INSTANCE = Mappers.getMapper(CoverLetterMapper.class);

    @Mapping(source="user_id", target = "user.user_id")
    CoverLetter mapCoverLetterDtoToCoverLetter(CoverLetterDto dto);
}

Because you’re wanting to map user_id from CoverLetterDto to the User class in CoverLetter, you have to use dot notation to access the user_id field in the user object.

Please let me know if you have additional questions.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement