Java 在JPA中使用oneToMany关系时,Postgresql在列中抛出null值违反了NOTNULL约束
我尝试测试Hibernate的一对多关系。我对邮政和会后实体的定义如下: Post.javaJava 在JPA中使用oneToMany关系时,Postgresql在列中抛出null值违反了NOTNULL约束,java,postgresql,hibernate,jpa,Java,Postgresql,Hibernate,Jpa,我尝试测试Hibernate的一对多关系。我对邮政和会后实体的定义如下: Post.java import javax.persistence.*; import java.util.ArrayList; import java.util.List; @Entity @Table(name = "post") public class Post { @Id @Column(name="post_id") @GeneratedValue(strategy= Genera
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "post")
public class Post {
@Id
@Column(name="post_id")
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long postId;
@Column(name="title")
private String title;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "post",
orphanRemoval = true)
private List<PostComment> comments = new ArrayList<>();
public Post() {};
public Post(String title) {
this.title = title;
}
// Add getter and setter
}
import javax.persistence.*;
@Entity
@Table(name = "post_comment")
public class PostComment {
@Id
@Column(name="comment_id")
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long commentId;
@Column(name="review")
private String review;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
public PostComment() {};
public PostComment(String review) {
this.review = review;
}
// Add getter and setter
}
public interface PostRepository extends JpaRepository<Post,Long> {
}
PostRepository.java
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "post")
public class Post {
@Id
@Column(name="post_id")
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long postId;
@Column(name="title")
private String title;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "post",
orphanRemoval = true)
private List<PostComment> comments = new ArrayList<>();
public Post() {};
public Post(String title) {
this.title = title;
}
// Add getter and setter
}
import javax.persistence.*;
@Entity
@Table(name = "post_comment")
public class PostComment {
@Id
@Column(name="comment_id")
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long commentId;
@Column(name="review")
private String review;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
public PostComment() {};
public PostComment(String review) {
this.review = review;
}
// Add getter and setter
}
public interface PostRepository extends JpaRepository<Post,Long> {
}
不幸的是,测试抛出了一个与null违犯约束相关的Postgresql错误:
Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "post_id" violates not-null constraint
Detail: Failing row contains (null, Post 1).
非常感谢您抽出时间。您似乎没有传递
post\u id
的值 在您的PostComment
类中,您有一个字段私人Post
。我怀疑你是否设置了那个字段
一个选项是将Post
作为构造函数参数添加到PostComment
类(以及默认构造函数),并将其与Post
对象一起实例化,如中所示:
public PostComment(String review, Post post) {
this.review = review;
this.post = post;
}
在构建对象时使用:
PostComment postComment1 = new PostComment(" Post comment 1", post);
而不是
PostComment postComment1 = new PostComment(" Post comment 1");
或者另一种方法是在Post
类中添加脚手架代码,如:
public void addPostComment(PostComment comment) {
comments.add(comment);
comment.setPost(this);
}
并在更新/添加
Post
类中的Post命令时使用此方法。我已通过删除现有数据库并使用spring.jpa.hibernate.ddl auto=create drop
生成新数据库解决了此问题。当然,只有在数据库中没有有价值的数据时,您才能这样做。完成后,将下面的代码更改回spring.jpa.hibernate.ddl auto=update
;您也可以在“子”中设置父引用
{ Post post = new Post(" Post 1");
PostComment postComment1 = new PostComment(" Post comment 1");
postComment1.setPost(post); // < -- here
PostComment postComment2 = new PostComment(" Post comment 2");
postComment2.setPost(post); // < -- here
post.setComments(Arrays.asList(postComment1,postComment2));
postRepository.save(post);
}
您没有传递post\u id
的值。能否确认数据库表具有您期望的列名?我遇到了同样的问题。一年半后有什么解决办法吗?谢谢你的回答。但是,我将post_id设置为auto@GeneratedValue。我认为我不需要为post_id添加值。此外,我尝试在“post post=new post(“post 1”);”之后添加“post.setPostId(1L)”,然后再次运行它。新异常引发“由以下原因引起:org.postgresql.util.PSQLException:错误:column comments1\u.post\u id不存在位置:335”您可以在设置注释之前先尝试保存post对象吗?是的,我尝试先保存它,然后更新PostComment,但相同的异常“column comments1\u.post\u id不存在”,谢谢您的解决方案。我这样做是因为“post_id”列中的null值违反了notnull约束”。我正在考虑我的liquibase。这不应该被否决,它当然有助于将新生成的表与手动创建的表进行比较。