In my SpringBoot application, I have two entities User
and Role
with
public class User {
private Long id;
private String email;
private String password;
private Set<Role> roles;
[...]
}
public class Role {
private Long id;
private String name;
private Set<User> users;
[...]
}
My DTOs looked quite similar until I realized this could lead to a recursion problem, when a user
has a field role
which has a field of the same user
, which has a field of the same role
, etc.
Therefore I decided to hand only the id
s my DTOs, so they would look like this
public class UserDto {
private Long id;
private String email;
private String password;
private List<Long> roleIds;
}
public class RoleDto {
private Long id;
private String name;
private List<Long> userIds;
}
My mappers were quite simple and used to look like this
import org.mapstruct.Mapper;
@Mapper
public interface UserMapper {
User userDtoToUser(UserDto userDto);
UserDto userToUserDto(User user);
List<UserDto> userListToUserDtoList(List<User> users);
}
import org.mapstruct.Mapper;
@Mapper
public interface RoleMapper {
Role roleDtoToRole(RoleDto roleDto);
RoleDto roleToRoleDto(Role Role);
List<RoleDto> roleListToRoleDtoList(List<Role> roles);
}
How would I change them so they would convert users
to/from userIds
and roles
to/from roleIds
?
question from:
https://stackoverflow.com/questions/65829746/use-mapstruct-to-convert-member-variable-to-id-and-vice-versa 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…