【问题标题】:Mapping circular issue in mapstructmapstruct中的映射循环问题
【发布时间】:2020-01-23 19:20:17
【问题描述】:

假设我有下一个实体:

public class Profile {

   private UUID id;
   //Other properties

   private ProfileUser user;

   //Getters and Setters of properties
}

public class ProfileUser {

    private String username, password;

    private UUID id;
    private String firstName, lastName;
    //Other properties

    private Set<Group> groups;
    private Set<Authority> authorities;
    private Profile profile;

    //Getters and Setters
}

public class Group {

    private String name;

    private Collection<ProfileUser> users;
    private Collection<Authority> authorities;

    //Getters and Setters
}

public class Authority {

    private String name;

    private Collection<ProfileUser> users;
    private Collection<Group> groups;

    //Getters and Setters
}

我需要将它映射到下一个 DTO:

public class ProfileServiceDTO {

    private UUID id;
    //Other properties

    private ProfileUserServiceDTO user;

    //Getters and Setters
}

public class ProfileUserServiceDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    private String username, password;

    private UUID id;
    private String firstName, lastName;
    //Other properties

    private Set<GroupServiceDTO> groups;
    private Set<AuthorityServiceDTO> authorities;

    //Getters and Setters
}

public class GroupServiceDTO {

    private String name;

    private Set<ProfileUserServiceDTO> users;
    private Set<AuthorityServiceDTO> authorities;
}

public class AuthorityServiceDTO implements Serializable {
     private static final long serialVersionUID = 1L;

     private String name;

     private Set<GroupServiceDTO> groups;
     private Set<ProfileUserServiceDTO> users;

     //Getters and setters
}

我需要将实体映射到 DTO,但要避免循环,

我如何以正确的方式执行此操作,以便在配置文件 DTO 下看到 ProfileUser DTO 及其组和权限??

现在我在配置文件映射器中使用下一个代码:

@Mapping(target = "user.groups", expression = "java(null)")
@Mapping(target = "user.authorities", expression = "java(null)")
public ProfileServiceDTO profileToProfileServiceDTO(Profile profile);

但上面的映射代码不返回组和权限,因为我将其设置为 null 以避免循环,那么我该如何解决这个问题?

【问题讨论】:

    标签: java spring-boot mapstruct


    【解决方案1】:

    我解决了这个问题:

    @Mapping(source = "user.groups", target = "user.groups", qualifiedByName = "profileAndGroup")
    @Mapping(source = "user.authorities", target = "user.authorities", qualifiedByName = "profileAndAuthority")
    public ProfileServiceDTO profileToProfileServiceDTO(Profile profile);
    
    @Named("profileAndGroup")
    @Mapping(target = "users", expression = "java(null)")
    @Mapping(target = "authorities", expression = "java(null)")
    GroupServiceDTO profileAndGroup(Group group);
    
    @Named("profileAndAuthority")
    @Mapping(target = "users", expression = "java(null)")
    @Mapping(target = "groups", expression = "java(null)")
    AuthorityServiceDTO profileAndAuthority(Authority authority);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-05
      • 2020-09-12
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 2020-08-02
      相关资源
      最近更新 更多