【发布时间】:2017-07-08 18:04:20
【问题描述】:
我尝试测试 Hibernate 的一对多关系。我将 Post 和 PostComment 实体定义如下: 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= 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
}
PostComment.java
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
}
PostRepository.java
public interface PostRepository extends JpaRepository<Post,Long> {
}
和db-changelog.xml
<changeSet id="1" author="dev">
<createTable tableName="post_comment">
<column name="comment_id" type = "bigint">
<constraints nullable="false" primaryKey="true"></constraints>
</column>
<column name="review" type="varchar(255)"></column>
</createTable>
<createTable tableName="post">
<column name="post_id" type="bigint">
<constraints nullable="false" primaryKey="true"></constraints>
</column>
<column name="title" type = "varchar(255)"></column>
</createTable>
</changeSet>
然后,我使用 SpringJUnit 在 PostServiceITTest.java 中添加新帖子
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.Arrays;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
@WebAppConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@ActiveProfiles("devmock")
public class PostServiceITTest {
@Autowired
private PostRepository postRepository;
@Test
public void testAddPost(){
Post post = new Post(" Post 1");
PostComment postComment1 = new PostComment(" Post comment 1");
PostComment postComment2 = new PostComment(" Post comment 2");
post.setComments(Arrays.asList(postComment1,postComment2));
postRepository.save(post);
}
}
不幸的是,测试抛出了一个与 null-violate 约束相关的 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_id传递值。 -
你能确认数据库表有你期望的列名吗?
-
我也遇到了同样的问题。一年半后有什么解决办法吗?
标签: java postgresql hibernate jpa