Reputation: 317
I have JPA entity with list like this:
@OneToMany(mappedBy = "scadaElement", orphanRemoval = true)
private List<ElementParameter> elementParameters;
and map form ElementParameter
@ManyToOne
@JoinColumn(name = "SCADAELEMENT_ID")
ScadaElement scadaElement;
when i get entity with elementParameters list and do stream on it stream do nothing, even when I trigger list with .size() but when I do the same with a for loop it work.
System.out.println("elements size: " + s.getElementParameters().size());
s.getElementParameters()
.stream()
.forEach(
a -> {
System.out.println("elementId: " + a.getId());
}
);
Is there any solution to make that stream work? I use eclipselink as JPA provider.
Upvotes: 10
Views: 3509
Reputation: 7581
Why not using the real JPA Streaming?
Stream<User> findAllByName(String name);
Upvotes: 1
Reputation: 298203
Apparently, you are referring to this issue. These lazy lists using the anti-pattern of inheriting from actual implementations (here Vector
) fail to adapt to the evolution of the base class. Note that there are two possible outcomes depending on how the anti-pattern was realized
Apparently, the second case applies to you. Triggering the population of the list does not make the inherited forEach
method work. Note that turning off the lazy population via configuration might be the simpler solution here.
To me, the cleanest solution would be if IndirectList
inherits from AbstractList
and adheres to the Collection API standard, now, almost twenty years after the Collection API has superseded Vector
(should I mention how much younger JPA actually is?). Unfortunately, the developers didn’t go that road. Instead, the anti-pattern was maxed out by creating another class that inherits from the class which already inherits from the class not designed for inheritance. This class overrides the methods introduced in Java 8 and perhaps gets another subclass in one of the next Java releases.
So the good news is, developers expecting every List
to be a Vector
do not have to make up their minds, but the bad news is it doesn’t work as sometimes, you will not get the extended Java 8 specific version with JPA 2.6. But apparently, JPA 2.7 will work.
So you can derive a few alternative solutions:
List<ElementParameter> workList=new ArrayList<>(elementParameters);
workList
will support all Collection & Stream operationsUpvotes: 10