Skip to content

Instantly share code, notes, and snippets.

@thieux
Last active October 11, 2019 13:00
Show Gist options
  • Save thieux/39363e613de7240e28a3680c149410ef to your computer and use it in GitHub Desktop.
Save thieux/39363e613de7240e28a3680c149410ef to your computer and use it in GitHub Desktop.
Shows that IndirectList (EclipseLink) does not respect Stream contract

EclipseLink Does Not Support Java 8 Stream

If you process a list provided by your DAO using a Stream, you'll be surprised!

List<Integer> list = new IndirectList();
list.add(1);
list.stream()
    .anyMatch(o -> o.equals(1)); // false

IndirectList is an implementation provided by EclipseLink that may be returned by your data access layer.

Version of eclipse link:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.0</version>
</dependency>

Other reference: https://stackoverflow.com/questions/35362581/stream-api-not-working-for-lazy-loaded-collections-in-eclipselink-glassfish

package com.example.demo;
import org.eclipse.persistence.indirection.IndirectList;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class IndirectListTest {
@Test
public void control() {
List<Integer> list = new ArrayList<>();
list.add(1);
assertTrue(list.stream()
.anyMatch(o -> o.equals(1)));
}
@Test
public void indirectList() {
@SuppressWarnings("unchecked")
List<Integer> list = new IndirectList();
list.add(1);
assertFalse(list.stream()
.anyMatch(o -> o.equals(1)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment