【发布时间】:2021-06-05 11:14:50
【问题描述】:
我有两个实体,帖子和评论。
发布实体:
@Entity
@Table(name = "posts")
public class Post {
@Id
@GeneratedValue
private Long id;
private String title;
private String content;
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();
评论实体:
@Entity
@Table(name = "comments")
public class Comment {
@Id
@GeneratedValue
private Long id;
private String content;
@ManyToOne
@JoinColumn(name = "post_id")
private Post post;
为了便于阅读,省略了 getter 和 setter。
当我通过 PostController 发送一个 POST 请求时,我可以在我的 PostgreSQL 数据库中存储一个新的 Post。如何通过控制器向这篇文章添加新评论?我似乎在任何地方都找不到答案。
【问题讨论】:
标签: java postgresql spring-boot http-post hibernate-onetomany