【问题标题】:How to correctly persist one-to-many to many relation如何正确地保持一对多对多的关系
【发布时间】:2021-09-03 09:50:45
【问题描述】:

我来了

原因:org.postgresql.util.PSQLException: ERROR: null value in “post_id”列违反非空约束

当我试图坚持关注实体时。 该关系是一个 Post 到多个 post cmets 的 Post cmets,后者有更多 post cmets。如果可能的话,我也希望这种关系是单向的。你能指出我做错了什么吗?

SQL:

    CREATE TABLE post
    (
     id    BIGSERIAL PRIMARY KEY,
     title TEXT
    );

    CREATE TABLE post_comment
    (
     id      BIGSERIAL PRIMARY KEY,
     review  TEXT,
     post_id BIGINT NOT NULL,

     CONSTRAINT post_comment_fk FOREIGN KEY (post_id)
        REFERENCES post (id)
    );

    CREATE TABLE more_post_comment
    (
     id      BIGSERIAL PRIMARY KEY,
     another  TEXT,
     post_comment_id BIGINT NOT NULL,

     CONSTRAINT more_post_comment_fk FOREIGN KEY (post_comment_id)
        REFERENCES post_comment (id)
    );

存储库:

    @Repository
    public interface PostRepository extends JpaRepository<Post, Long> {
    }

帖子:

    @Entity(name = "Post")
    @Table(name = "post")
    @Data
    public class Post {
 
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String title;
 
        @OneToMany(cascade = CascadeType.ALL)
        @JoinColumn(name = "post_id")
        private List<PostComment> comments = new ArrayList<>();

     }

发表评论:

    @Entity(name = "PostComment")
    @Table(name = "post_comment")
    @Data
    public class PostComment {
 
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String review;

        @OneToMany(cascade = CascadeType.ALL)
        @JoinColumn(name = "post_comment_id")
        private List<MorePostComment> morePostComment = new ArrayList<>();
    }

更多帖子评论:

    @Entity(name = "MorePostComment")
    @Table(name = "more_post_comment")
    @Data
    public class MorePostComment {
 
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       private Long id;
       private String another;
   }

测试:

@SpringBootTest
class Test {

    @Autowired
    private PostRepository postRepository;

    @Test
    public void should() {

        Post post = new Post();
        post.setTitle("title");

        MorePostComment morePostComment = new MorePostComment();
        morePostComment.setAnother("another");

        List<MorePostComment> morePostComments = new ArrayList<>();
        morePostComments.add(morePostComment);

        PostComment postComment = new PostComment();
        postComment.setReview("asd");
        postComment.setMorePostComment(morePostComments);

        List<PostComment> comments = new ArrayList<>();
        comments.add(postComment);
        post.setComments(comments);

        post = postRepository.save(post);

        assertThat(post.getComments()).isNotEmpty();
        assertThat(post.getComments().get(0).getId()).isNotNull();
        assertThat(post.getComments().get(0).getMorePostComment()).isNotEmpty();

    }
}

【问题讨论】:

    标签: java hibernate jpa spring-data


    【解决方案1】:

    请将nullable = false 添加到您的@JoinColumn 注释中:

        @OneToMany(cascade = CascadeType.ALL)
        @JoinColumn(name = "post_id", nullable = false)
        private List<PostComment> comments = new ArrayList<>();
    

        @OneToMany(cascade = CascadeType.ALL)
        @JoinColumn(name = "post_comment_id", nullable = false)
        private List<MorePostComment> morePostComment = new ArrayList<>();
    

    提示:您不需要在 PostRepository 上方的 @Repository 注释,因为它扩展了 JpaRepostirory 并且 spring 知道应该创建存储库 bean。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 2011-03-07
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多