Pet*_*r T 5 java postgresql hibernate jpa
我尝试测试 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
}
Run Code Online (Sandbox Code Playgroud)
发表评论.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
}
Run Code Online (Sandbox Code Playgroud)
PostRepository.java
public interface PostRepository extends JpaRepository<Post,Long> {
}
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
然后,我使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,测试抛出了与空违反约束相关的 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).
Run Code Online (Sandbox Code Playgroud)
我非常感谢您的宝贵时间。
| 归档时间: |
|
| 查看次数: |
10165 次 |
| 最近记录: |