Карта круговой проблемы в mapstruct - PullRequest
0 голосов
/ 21 января 2020

При условии, что у меня есть следующие объекты:

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 ??

Теперь я использую следующий код в профиле mapper:

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

, но вышеприведенный код отображения не возвращает группы и права доступа, потому что я установил его в null, чтобы избежать циклического перехода, так как я могу решить эту проблему?

1 Ответ

0 голосов
/ 21 января 2020

Я решил это с помощью:

@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);
...