gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:04 26 4
gpt4 key购买 nike

I have one Entity class called RCIEntity. which has a One to many mapping to its own type.

@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: First count0, 2nd count 4 **

最佳答案

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

关于Java Stream 与 JPA 一对多(LAZY)关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33151787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com