【问题标题】:Java Stream with JPA one to Many(LAZY) Relation not working [duplicate]具有JPA一对多(LAZY)关系的Java Stream不起作用[重复]
【发布时间】:2015-10-15 14:53:19
【问题描述】:

我有一个名为 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 **

【问题讨论】:

  • 您在使用 EclipseLink 吗?是不是和here一样的问题?
  • 是的,它似乎是 EclipsLink 2.5.2 版的错误。所以我将版本更改为 2.6.0 并且工作正常。感谢你的回答。 bugs.eclipse.org/bugs/show_bug.cgi?id=433075

标签: java hibernate jpa java-stream


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 2018-04-04
    • 2021-02-05
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    相关资源
    最近更新 更多