gpt4 book ai didi

java - 关系一对多返回空集

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:21 26 4
gpt4 key购买 nike

这是我的 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);
}

我正在尝试打印带有所有评论的帖子,但评论集返回 null

它返回的响应为:

addPost: 帖子{postId=null, postName='politics', comments=[]}

帖子{postId=1, postName='politics', comments=[]}

最佳答案

您禁止更新评论实体中的帖子。删除可插入和可更新属性

@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 +
'}';
}

我还建议不要使用急切获取并阅读如何进行双向关联。

关于java - 关系一对多返回空集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49873523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com