Mapping circular issue in mapstruct

mibrahim.iti

Assuming i have next entities:

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
}

And i need to map it to next DTOs:

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
}

And i need to map entities to DTOs but with avoiding circular,

how i do this in a correct way so i can see ProfileUser DTO with its groups and authorities under profile DTO ??

Now i am using next code in profile mapper:

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

but above mapping code doesn't return groups and authorities because i put it to null to avoid circular, so how i can fix this problem?

mibrahim.iti

I solved it by using:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related