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(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
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时,得到无限递归。即使我没有从接收的对象中请求相关的注释实体,我仍然会在调试模式下看到无限嵌套,尽管我选择了[]
获取= FetchType.LAZY
谁有解决此问题的想法,为什么会这样工作?
实体实体@Table(name =“ posts”)公共类Post {@Id @SequenceGenerator(name =“ jpaSequence.Post”,sequenceName =“ SEQUENCE_POST”,allocationSize = 1)@ ...
解决方案>>