我试图返回 JPA 数据(当然,转换为 DTO),其中具有 @OneToMany
和 @ManyToOne
双向关系。我目前正在申请东西fix 。问题是输出是递归的。评论有帖子,有评论,然后有帖子(评论 -> 帖子 -> 评论 -> 等等..)。
我只想拥有这样的东西
{
"post_id": 1
"user": {
// user data
},
"description": "some description",
"images": "{images,images}",
"tags": "{tags, tags}",
"comments": [
{
//some comments data
},
{
//some comments data
}
]
"lastUpdated": "2020-04-08T14:23:18.000+00:00"
}
这是我的代码
这是我的 Posts.class
@Data
@Entity
@Table(name = "posts")
@EntityListeners(AuditingEntityListener.class)
public class Posts {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "post_id")
private Long post_id;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private Users user;
@Column(name = "description")
private String description;
@Column(name="images")
private String images;
@Column(name="tags")
private String tags;
@OneToMany(
fetch = FetchType.LAZY,
mappedBy = "post",
cascade = CascadeType.ALL,
orphanRemoval = true
)
@JsonIgnoreProperties(value = { "comments" ,"hibernateLazyInitializer", "handler" }, allowSetters = true)
//@JsonManagedReference
private List<Comments> comments;
@LastModifiedDate
@Column(name = "last_updated")
private Date lastUpdated;
public void addComments(Comments comment) {
this.comments.add(comment);
}
}
这是我的 PostDTO.class
@Data
public class PostDTO {
private Long post_id;
private UserDTO user;
private String description;
private String images;
private String tags;
private List<CommentsDTO> comments;
private Date lastUpdated;
}
这是我的 Comments.class
@Data
@Entity
@Table(name = "comments")
@EntityListeners(AuditingEntityListener.class)
public class Comments {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="comment_id")
private Long comment_id;
@OneToOne
@JoinColumn(name = "user_id")
private Users user;
@Column(name = "description")
private String description;
@Column(name="images")
private String images;
@ManyToOne
@JoinColumn(name ="post_id" , nullable = false)
@JsonIgnoreProperties(value = { "post" ,"hibernateLazyInitializer", "handler" }, allowSetters = true)
//@JsonBackReference
private Posts post;
@Column(name="last_updated")
@LastModifiedDate
private Date lastUpdated;
}
这是我的 CommentsDTO.class
@Data
public class CommentsDTO {
private Long comment_id;
private UserDTO user;
private String description;
private PostDTO post;
private String images;
private Date lastUpdated;
}
这是我的 REST Controller
@GetMapping
public @ResponseBody ResponseEntity<List<PostDTO>> getAll() throws Exception {
return new ResponseEntity<List<PostDTO>>(service.getAll(), HttpStatus.OK);
}
这是我的服务
public List<PostDTO> getAll() throws Exception {
return repo.findAll()
.stream()
.map(this::convert)
.collect(Collectors.toList());
}
private PostDTO convert(Posts e) {
return mapper.map(e, PostDTO.class);
}
希望有人能解答我的问题。这次有点输了。
最佳答案
问题是,当您将 Post
转换为 PostDTO
时,然后通过使用 ModelMapper,它会为 PostDTO
调用 Post 的所有字段的 getter。所以这递归地发生了
mapper.map(e, PostDTO.class)
因此,只需从 CommentDTO 中删除 private PostDTO post
即可,
那么 modelmapper 不要尝试设置 PostDTO->comment->post 字段。
而且 DTO 中不需要双向关系。 DTO 就是您想要在响应中显示的内容。
关于java - Spring JPA 一对多无限递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61114583/