I am tring to mapping entity datas to DTOs using mapstruct. And with these sources, I could map id,title datas. But the problem is.... I can not map userName using these sources.
How can I resolve this problem??
@Entity // DB와의 연결을 위하여
@Data // getter setter
public class Board {
@Id // id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(min=2, max=30)
private String title;
@Length(min=20)
@Lob
@Column(columnDefinition="TEXT", nullable = false)
private String content;
@ManyToOne
@JoinColumn(name="userId", referencedColumnName = "id")
private User user;
}
@Builder
@AllArgsConstructor
@Data
public class BoardListDto {
private Long id;
private String title;
private String userName;
}
@Mapper(componentModel = "spring")
public interface BoardListMapper extends EntityMapper<BoardListDto, Board> {
@Override
@Mapping(target = "userName", source = "user.name.value")
List<BoardListDto> toDtos(List<Board> board);
}
public interface EntityMapper <D, E> {
E toEntity(D dto);
D toDto(E entity);
// Entity업데이트 시 null이 아닌 값만 업데이트 하도록 함.
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
void updateFromDto(D dto, @MappingTarget E entity);
List<D> toDtos(List<E> entity);
}
no need to implement toDtos method for this. This code should be enough and Mapstruct will handle the rest alone.
@Mapper(componentModel = "spring")
public interface BoardListMapper extends EntityMapper<BoardListDto, Board> {
@Override
@Mapping(target = "userName", source = "user.name")
BoardListDto toDto(Board board);
}