プレイフレームワークのYet Another Blog Engine exampleは、子どもたちのコメントとPostクラスがあります。@OneToManyリストはいつPlayフレームワークに移入されますか?
// 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("[email protected]", "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()を呼び出すことでした。
JPA.em()。clear()を使用してHibernateキャッシュをクリアし、強制的にオブジェクトをDBから取得する必要があることを知りました。 – tba