2015-10-15 76 views
1

我有一個名爲RCIEntity的實體類。它有一對多映射到它自己的類型。Java流與JPA一對多(LAZY)關係不起作用

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
@OrderBy("name ASC")   
private List<RCIEntity> children; 

public final List<RCIEntity> getChildren() { 
     return children; 
} 

所以,當我在其他地方使用該實體他們。

rciEnitity.getChildren().stream().count() // always zero. 

但如果我從它創建一個子列表,然後流正常工作。

List<RCIEntity> subList = get.getChildren().subList(0,get.getChildren().size()); 

long count1 = subList.stream().count();//works fine 

下面是一些代碼...

CriteriaBuilder cb = em.getCriteriaBuilder(); 
     CriteriaQuery<RCIEntity> q = cb.createQuery(RCIEntity.class); 
     Root<RCIEntity> from = q.from(RCIEntity.class); 

     CriteriaQuery<RCIEntity> select = q.select(from); 
     q.select(from).where(cb.equal(from.get(RCIEntity_.project), true)); 

     List<RCIEntity> resultList = em.createQuery(q).getResultList(); 

     RCIEntity get = resultList.get(0); 

     **long count = get.getChildren().stream().count(); // output 0. Doesn't work** 

     System.out.println("First count"+count); 
     List<RCIEntity> subList = get.getChildren().subList(0, get.getChildren().size()); 
     **long count1 = subList.stream().count(); // works fine output 4.** 

     System.out.println("2nd count "+count1); 

** OUTPUT:一是COUNT0, 第二計數4 **

+0

您使用的是EclipseLink嗎?是不是像[這裏](http://stackoverflow.com/a/31941225/4856258)一樣的問題? –

+0

好像它是EclipsLink 2.5.2版的一個bug。所以我將版本更改爲2.6.0並且工作正常。感謝你的回答。 https://bugs.eclipse.org/bugs/show_bug.cgi?id=433075 –

回答

0

所以最後它的EclipseLink的一個bug版本2.5.2。並在2.6.0中修復