Java流与JPA一对多(LAZY)关系不起作用

时间:2015-10-15 14:53:20

标签: java hibernate jpa java-stream

  

我有一个名为RCIEntity的实体类。其中有一对多映射到自己的类型。

@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);
  

**输出:首先count0,                第二次计数4 **

1 个答案:

答案 0 :(得分:0)

所以最后它是EclipseLink版本2.5.2的一个错误。并在2.6.0中修复

相关问题