Бесконечная рекурсия при использовании @OneToMany и @ManyToOne + MapStruct

  • entity
Entity
@Table(name ="posts")
public class Post {

    @Id
    @SequenceGenerator(name = "jpaSequence.Post",
            sequenceName = "SEQUENCE_POST",
            allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "jpaSequence.Post")
    private Long id;
    private String subject;

    @OneToMany(mappedBy = "post", fetch = FetchType.LAZY)
    private List<Comment> comments = new ArrayList<>();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn
    private User user;


    public Post() {
    }
@Entity
@Table(name = "comments")
public class Comment {

    @Id
    @SequenceGenerator(name = "jpaSequence.Comment",
            sequenceName = "SEQUENCE_COMMENT",
            allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "jpaSequence.Comment")
    private Long id;

    private String reply;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn
    private Post post;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn
    private User user;


    public Comment() {
    }
@Entity
@Table(name = "users")
public class User {

    @Id
    @SequenceGenerator(name = "jpaSequence.User",
            sequenceName = "SEQUENCE_USER",
            allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "jpaSequence.User")
    private Long id;

    private String name;
    private String email;


    public User() {
    }
  • mapper
@Mapper(componentModel = "spring" )
public interface PostMapper {

    Post postDtoToPostEntity(PostDto dto);

    PostDto postEntityToPostDto(Post entity);

    Iterable<Post> postListDtoToPostListEntity(Iterable<PostDto> list);

    Iterable<PostDto> postListEntityToPostListDto(Iterable<Post> list);
}

@Mapper(componentModel = "spring")
public interface CommentMapper {

    Comment commentDtoToCommentEntity (CommentDto dto);

    CommentDto commentEntityToCommentDto(Comment entity);

    Iterable<Comment> commentListDtoToCommentListEntity(Iterable<CommentDto> list);

    Iterable<CommentDto> commentListEntityToCommentListDto(Iterable<Comment> list);
}
  • service
@Service
public class PostReadServiceImpl implements PostReadService {

    private PostRepository repository;

    private PostTransformer transformer;

    private CommentTransformer transformerComment;

    @Autowired
    public PostReadServiceImpl(PostRepository repository, PostTransformer transformer,
                               CommentTransformer transformerComment) {
        this.repository = repository;
        this.transformer = transformer;
        this.transformerComment = transformerComment;
    }


    @Transactional
    @Override
    public PostDto getEntryById(Long id) {

        Post entity = find(id);

        return this.transformer.transformEntityToDto(entity);
    }


    private Post find(Long id){

        return this.repository.findById(id).orElseThrow(() -> new RuntimeException("Post not found!"));
    }


    @Transactional
    @Override
    public Iterable<CommentDto> getListEntriesCommentById(Long id) {

        Iterable<Comment> listComment = findListComment(id);

        Iterable<CommentDto> commentDtoList = this.transformerComment.transformListEntityToDtoList(listComment);

        return commentDtoList;
    }

    private Iterable<Comment> findListComment(Long id){

        Post post = this.repository.findById(id).orElseThrow(() -> new RuntimeException("Post not found!"));

        List<Comment> comments = post.getComments();


        return comments;
    }


}

При использовании mapStruct, я получаю бесконечную рекурсию.

Даже если бы я не запрашивал из полученного объекта, связанную сущность Comments, я в debug mode, все равно виже бесконечную вложенность и это несмотря на то, что выбран

fetch = FetchType.LAZY

Кто знает как это исправить?


Ответы (0 шт):