so i have a social network project with entities such as User and Post. I have a UserResponse and a PostResponse too, so im trying to map them using mapstruct but im getting this cyclic dependency error.
User:
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String fullName;
private String username;
private String email;
private String password;
private String bio;
@Lob
private byte[] profilePictureUrl;
private String role;
private long followerCount;
private long followingCount;
private String accountPrivacy; // public, private
@OneToOne(cascade = CascadeType.ALL)
private VerificationCode verificationCode;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Like> likes;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Reel> reels;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Story> stories;
@OneToMany(cascade = CascadeType.ALL)
private Set<UserEntity> followers;
@OneToMany(cascade = CascadeType.ALL)
private Set<UserEntity> followings;
@OneToMany(cascade = CascadeType.ALL)
private Set<UserEntity> pendingFollowRequests;
}
User Response:
public class UserResponse {
private String username;
private String fullName;
private String bio;
private byte[] profilePicture;
private String accountPrivacy;
private long followerCount;
private long followingCount;
private List<PostResponse> posts;
private List<ReelResponse> reels;
private List<Story> stories;
private List<CommentResponse> comments;
private Set<LikeResponse> likes;
private Set<UserResponse> followers;
private Set<UserResponse> followings;
}
user mapper:
@Service
@Mapper(componentModel = "spring",
injectionStrategy = InjectionStrategy.SETTER,
uses = {PostMapper.class, ReelMapper.class, StoryMapper.class, LikeMapper.class, CommentMapper.class})
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
@Lazy
@Named("userResponseToUser")
@Mapping(source = "profilePicture", target = "profilePictureUrl")
@Mapping(source = "posts", target = "posts", qualifiedByName = "postResponseToPost")
@Mapping(source = "reels", target = "reels", qualifiedByName = "reelResponseToReel")
@Mapping(source = "comments", target = "comments", qualifiedByName = "commentResponseToComment")
@Mapping(source = "likes", target = "likes", qualifiedByName = "likeResponseToLike")
@Mapping(source = "followers", target = "followers", qualifiedByName = "userResponseToUser")
@Mapping(source = "followings", target = "followings", qualifiedByName = "userResponseToUser")
UserEntity userResponseToUser( UserResponse userResponse);
@Lazy
@Named("userToUserResponse")
@Mapping(source = "profilePictureUrl", target = "profilePicture")
@Mapping(source = "posts", target = "posts", qualifiedByName = "postToPostResponse")
@Mapping(source = "reels", target = "reels", qualifiedByName = "reelToReelResponse")
@Mapping(source = "comments", target = "comments", qualifiedByName = "commentToCommentResponse")
@Mapping(source = "likes", target = "likes", qualifiedByName = "likeToLikeResponse")
@Mapping(source = "followers", target = "followers", qualifiedByName = "userToUserResponse")
@Mapping(source = "followings", target = "followings", qualifiedByName = "userToUserResponse")
UserResponse userToUserResponse( UserEntity userEntity);
}
so what should i do here to fix this problem?
I have tried these solutions with @Named and such, even used Injection strategy of type setter yet it still exists
iM- Fr0sty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.