2011-01-12 3 views
1

놀이방 프레임 워크의 Yet Another Blog Engine example가 어린이 댓글과 게시물 클래스 : 데이터가 .yml에 의해 채워집니다Play 프레임 워크에서 @OneToMany 목록은 언제 채워 집니까?

// Post.java 
// ... other fields, etc. 
@OneToMany(mappedBy="post", cascade=CascadeType.ALL) 
public List<Comment> comments; 
// ... 


이 모든 것이 잘 작동하는 것 같다 :

// 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 
} 


그러나 수동으로 포스트와 데이터베이스에 대한 일부 의견을 저장할 때 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()를 호출하는 것이 었습니다.

답변

0

JPA에서 양방향 관계를 사용할 때 메모리 내 객체의 관계 양쪽에서 일관성을 유지하는 것은 사용자가 결정합니다.

나는 프레임 워크 find()를 구현 플레이 방법을 잘 모르겠지만, frontPostp 같은 인스턴스 인 경우 (즉하지 데이터베이스 데이터로 구성 새로운 인스턴스), 그것은 comments 컬렉션 확실히 비어 있습니다.

+0

감사합니다. JPA.em(). clear()를 사용하여 Hibernate 캐시를 지우고 개체를 DB에서 강제로 가져와야 함을 알았습니다. – tba

1

전체 지속성 컨텍스트을 지우고 다른 모든 엔티티를 분리하는 대신 em.refresh (p)를 호출하려고합니다.

그러나 이것은 db를 쿼리하는 것과 관련이 있습니다. 그래서 나중에 튜토리얼에서 Play가 도우미 메서드를 구현합니다.이 튜토리얼에서는 여행을 저장하기 위해 새 주석을 유지 한 후 메모리 내 엔티티를 업데이트합니다.

관련 문제