【发布时间】:2020-04-09 05:35:15
【问题描述】:
我试图返回一个具有@OneToMany 和@ManyToOne 双向关系的JPA 数据(当然,转换为DTO)。我目前申请的东西fix。问题是输出是可恢复的。 cmets 有帖子有 cmets 然后有帖子(cmets -> 帖子 -> 评论 -> 等等..)。
我只想拥有这样的东西
{
"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 控制器
@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);
}
希望有人能阐明我的问题。到现在为止有点迷失了。
【问题讨论】:
-
您需要最高 n 级的数据吗?
-
对不起,我用我的预期输出更新了帖子
-
你可以试试
@JsonIgnoreProperties(value = { "post" ,"hibernateLazyInitializer", "handler" }, allowSetters = true) private List<Comments> comments;For post 也改一样试试 -
是一样的:(
-
你能在代码中更新你所做的吗?
标签: java spring spring-data-jpa jackson