Issue
I have a problem when mapping a List from a List. All default fields of Entity are well mapped.
ex) (Entity)String date -> (DTO) String date
However, the Join object field exists in Entity.
We need to pull the data out of this object field and map it anew.
In the case of a single Entity to single DTO rather than a List to List, this was easily possible.
@Mapping(target = ".", source = "user")
This way we were able to map all fields of the user object field that the Entity has to the remaining unmapped fields.
However, trying the same on a List has no effect.
Which method should I use?
@Entity(name = "refund")
public class RefundEntity extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int refundId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private UserEntity user;
private int refundAmount;
}
and
public class AdminRefundUserDto {
@AllArgsConstructor
@Getter
public static class Response {
private int refundAmount;
private String marketName;
private String bizNumber;
private String bizUserName;
}
}
and
@Entity(name = "user")
public class UserEntity extends BaseEntity {
@Id
@GeneratedValue(generator = "uuid4")
@GenericGenerator(name = "uuid", strategy = "uuid4")
@Column(columnDefinition = "BINARY(16)")
private UUID userId;
private String userName;
private String password;
private String phoneNumber;
private String marketName;
private String bizUserName;
private String bizNumber;
}
and I used
@Mapping(target = ".", source = "refundList.user")
List<AdminRefundUserDto.Response> toDtoList(List<RefundEntity> refundList);
Solution
First of all, create a method for the mapper with a SourceObject parameter that return a TargetObject
@Named("toResponseDto")
//add your mapping
AdminRefundUserDto.Response toResponseDto(RefundEntity refundEntity);
if you have complex logic of mapping, you can also create a custom method to map a certain target parameter: see Custom Mapper with MapStruct
Then add an IterableMapping
@IterableMapping(qualifiedByName = "toResponseDto")
List<AdminRefundUserDto.Response> toDtoList(List<RefundEntity> refundList);
Answered By - Paul Marcelin Bejan
Answer Checked By - David Goodson (JavaFixing Volunteer)