[英]Relationship one-To-Many return empty set
这是我的Post类,与OneToMany有关系
@Entity
@Table(name = "post")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "post_id")
private Integer postId;
@Column(name = "post_name")
private String postName;
@OneToMany(mappedBy = "post",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<Comment> comments = new HashSet<>();
@Override
public String toString() {
return "Post{" +
"postId=" + postId +
", postName='" + postName + '\'' +
", comments=" + comments +
'}';
}
public Integer getPostId() {
return postId;
}
public void setPostId(Integer postId) {
this.postId = postId;
}
public String getPostName() {
return postName;
}
public void setPostName(String postName) {
this.postName = postName;
}
public Set<Comment> getComments() {
return comments;
}
public void setComments(Set<Comment> comments) {
this.comments = comments;
}
}
并与每个帖子有关,我有多个注释,因为/ *我希望将结果作为帖子并与其相关,
* /
@Entity
public class Comment {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer commentId;
@Column(name = "comment")
private String comment;
@ManyToOne
@JoinColumn(name = "post_id",referencedColumnName = "post_id",insertable = false,updatable = false)
private Post post;
@Override
public String toString() {
return "Comment{" +
"commentId=" + commentId +
", comment='" + comment + '\'' +
", post=" + post +
'}';
}
public Integer getCommentId() {
return commentId;
}
public void setCommentId(Integer commentId) {
this.commentId = commentId;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
}
这些是存储库
public interface CommentRepository extends JpaRepository<Comment,Integer> {
List<Comment> findAllByPostPostId(Integer postId);
}
public interface PostRepository extends JpaRepository<Post,Integer> {
}
这些是我的其余映射
@Autowired
private PostRepository postRepository;
@Autowired
private CommentRepository commentRepository;
@RequestMapping(value= "/post" , method = RequestMethod.POST)
public void addPost(@RequestBody Post post){
System.out.println("addPost: " +post);
postRepository.save(post);
}
@RequestMapping(value= "/comment/{postId}" , method = RequestMethod.POST)
public void addComment(@RequestBody Comment comment,@PathVariable Integer postId){
Post post = postRepository.findOne(postId);
comment.setPost(post);
System.out.println("addComment: " +comment);
commentRepository.save(comment);
}
输出:
// addComment:Comment {commentId = null,comment ='modi',post = Post {postId = 1,postName ='politics',comments = []}}
@RequestMapping(value = "/post/{postId}",method = RequestMethod.GET)
public void getComments(@PathVariable Integer postId){
Post post = postRepository.findOne(postId);
System.out.println(post);
}
我正在尝试使用所有评论打印Post,但comment Set返回null
它返回响应为:
addPost:帖子{postId = null,postName ='politics',评论= []}
帖子{postId = 1,postName ='politics',评论= []}
当您的代码显示时,它是lazy
。 如果要通过获取post
实体来获取它,请将其更改为eager
。
你也应该更正mappedBy
来post
。
@OneToMany(mappedBy = "post",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<Comment> comments = new HashSet<>();
您都不应该在两个表中都使用toString方法来创建循环调用
尝试从Comment类中删除toString()
@Entity
public class Comment {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer commentId;
@Column(name = "comment")
private String comment;
@ManyToOne
@JoinColumn(name = "post_id" , referencedColumnName = "post_id", insertable = false,updatable = false)
private Post post;
public Integer getCommentId() {
return commentId;
}
public void setCommentId(Integer commentId) {
this.commentId = commentId;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
}
您禁止更新评论中的实体。 删除可插入和可更新的属性
@Entity
public class Comment {
@ManyToOne
@JoinColumn(name = "post_id",referencedColumnName = "post_id")
private Post post;
}
然后在toString()方法中中断循环引用,以避免StackOverflowException。
@Override
public String toString() {
return "Comment{" +
"commentId=" + commentId +
", comment='" + comment + '\'' +
// ", post=" + post +
'}';
}
我还建议不要使用热切获取,而是阅读如何进行双向关联。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.