Play框架的Yet Another Blog Engine example有一个带有子项的Post类注释:
// Post.java
// ... other fields, etc.
@OneToMany(mappedBy="post", cascade=CascadeType.ALL)
public List<Comment> comments;
// ...
当.yml填充数据时,一切似乎都运行良好:
// BasicTest.java
@Test
public void fullTest() {
Fixtures.load("data.yml");
// ...
// Find the most recent post
Post frontPost = Post.find("order by postedAt desc").first();
assertNotNull(frontPost);
// ...
// Check that this post has two comments
assertEquals(2, frontPost.comments.size()); // succeeds
}
但是当我手动将Post和一些注释保存到数据库时,frontPost.comments字段为空:
@Test
public void myFullTest() {
// Fixtures.load("data.yml");
User u = new User("bob@gmail.com", "secret", "Bob").save();
Post p = new Post(u, "About the model layer", "The model has a central position in a Play! application. It is the ...").save();
Comment c1 = new Comment(p, "Guest", "You are right !").save();
Comment c2 = new Comment(p, "Mike", "I knew that ...").save();
// Find the most recent post
Post frontPost = Post.find("order by postedAt desc").first();
// This assertion fails as frontPost.comments is empty:
// "Failure, expected:<2> but was <0>"
assertEquals(2, frontPost.comments.size());
}
为什么会发生这种情况,如何在逐个保存类时让JPA填充Post.comments字段?
全部谢谢!
更新:解决方法是在find(....)调用之前调用JPA.em()。clear()。
答案 0 :(得分:1)
您希望调用em.refresh(p),而不是清除整个持久性上下文并分离所有其他实体。
然而,这确实涉及查询数据库,因此稍后在本教程中实现一个帮助器方法,该方法将在保留新注释后更新内存中实体,以便为您节省行程。
答案 1 :(得分:0)
在JPA中使用双向关系时,保持内存中对象关系两边的一致性取决于您。
我不确定Play Framework如何实现find()
,但如果frontPost
与p
是同一个实例(即不是从数据库数据构造的新实例),那么它是{{ 1}}收集肯定是空的。