public class boardEntity {
@Id
private UUID boardId;
private String content;
private UserInfoEntity user;
}
and
public class UserInfoEntity {
@Id
private UUID userId;
private String userName;
private String userEmail;
private String userPhone;
}
I want Result
public class ResponseDto {
private UUID boardId; // << BoardEntity Field
private String content; // << BoardEntity Field
private UUID userId; // << BoardEntity in UserInfoEntity Field
private String userName; // << BoardEntity in UserInfoEntity Field
...
}
I am using mapstruct.
I have a UserEntity inside a BoardEntity. (join)
The DTO I want is to take out all the fields in each entity and create them.
Take out boardId and content from BoardEntity,
Get userId and userName from UserEntity in BoardEntity.
Create a DTO with these 4 fields, this is what I want.
It's not that I don't know how.
@Mapping(target = "userId", source = "user.userId")
@Mapping(target = "userName", source = "user.userName")
ResponseDto toDto (BoardEntity board);
I've currently implemented it this way and it works without problems. (Reproduced from memory, so it may be slightly different, but the method is the same.)
The problem is that I only wrote 2 @Mapping right now, but there are actually more fields to fill out.
And they're all doing manual mapping, so I think something's wrong.
Is there another way to get all objects inside Entity and map them to fields in Dto? (The field names are the same.)
CodePudding user response:
MapStruct offers a way to define mapping of unmapped target properties from a certain source.
e.g. in your example you would like to map the unmapped target properties userId
and userName
from the UserInfoEntity
into the ResponseDto
you can do that with a single @Mapping
@Mapping(target = ".", source ="user")
ResponseDto toDto(BoardEntity board);