关系一对多返回空集

时间:2018-04-17 08:41:55

标签: java spring

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

这些是我的Rest贴图

@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 =&#39; modi&#39;,post = Post {postId = 1,postName =&#39; politics&#39;,comments = []}} < / p>

    @RequestMapping(value = "/post/{postId}",method = RequestMethod.GET)
    public void getComments(@PathVariable Integer postId){
        Post post = postRepository.findOne(postId);
        System.out.println(post);
    }

并且我正在尝试使用它打印Post所有评论但注释Set返回null

它返回响应为:

addPost:发布{postId = null,postName =&#39; politics&#39;,comments = []}

发布{postId = 1,postName =&#39; politics&#39;,comments = []}

3 个答案:

答案 0 :(得分:0)

代码显示为lazy。如果您想通过提取post实体来获取它,请将其更改为eager

您也应该将mappedBy更正为post

@OneToMany(mappedBy = "post",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<Comment> comments = new HashSet<>();

答案 1 :(得分:0)

您不应在两个表中使用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;
    }

}

答案 2 :(得分:0)

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

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

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