Search code examples
javajpaejbjava-8eclipselink

Collections.sort doesn't invoke in java 8


I have EJB facade generated by NetBeans and sorting collections in entities fetched by JPA doesn't work. Here is the code:

@Stateless
public class CompetitionFacade extends AbstractFacade<Competition> implements CompetitionFacadeLocal {

@PersistenceContext(unitName = "mot_persistence_unit")
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
    return em;
}

public CompetitionFacade() {
    super(Competition.class);
}

@Override
public Competition findAndInitializeGD(Integer idCompetition) {
    Competition entity = em.find(Competition.class, idCompetition);

    entity.getGroupDetailsList().forEach(p -> System.out.println("First lambda " + p.getIdGroupName()));
    new ArrayList<>(entity.getGroupDetailsList()).forEach(p -> System.out.println("Second lambda " + p.getIdGroupName()));

    Collections.sort(entity.getGroupDetailsList(), new Comparator<GroupDetails>() {

        @Override
        public int compare(GroupDetails o1, GroupDetails o2) {
            throw new UnsupportedOperationException("First sorting");
        }

    });

    Collections.sort(new ArrayList<>(entity.getGroupDetailsList()), new Comparator<GroupDetails>() {

        @Override
        public int compare(GroupDetails o1, GroupDetails o2) {
            throw new UnsupportedOperationException("Second sorting");
        }

    });

    return entity;
}

}

AbstractFacade:

public abstract class AbstractFacade<T> {
private Class<T> entityClass;

public AbstractFacade(Class<T> entityClass) {
    this.entityClass = entityClass;
}

protected abstract EntityManager getEntityManager();

// auto-generated methods like find/edit etc.
}

Invoking this method gives this exception:

Caused by: java.lang.UnsupportedOperationException: Second sorting

Why isn't first exception thrown from sorting? Also it looks like lambda expression doesn't work either. After adding these two lines

entity.getGroupDetailsList().forEach(p -> System.out.println("First lambda " + p.getIdGroupName()));
new ArrayList<>(entity.getGroupDetailsList()).forEach(p -> System.out.println("Second lambda " + p.getIdGroupName()));

output is

Info:   Second lambda entities.GroupName[ idGroupName=709 ]
Info:   Second lambda entities.GroupName[ idGroupName=706 ]
Info:   Second lambda entities.GroupName[ idGroupName=707 ]
Info:   Second lambda entities.GroupName[ idGroupName=708 ]

EDIT:

This is field from the entity

@OneToMany(cascade = CascadeType.ALL, mappedBy = "competition")
private List<GroupDetails> groupDetailsList = new ArrayList<>();

with normal getter

public List<GroupDetails> getGroupDetailsList() {
    return groupDetailsList;
}

EDIT2: I found satisfying answer here https://stackoverflow.com/a/27980037/3803447 after accepted answer which hinted me right way.


Solution

  • If you have lazy loading enabled (which is the default), then the Collection returned by getGroupDetailsList is actually a special wrapper class provided by your JPA provider to act a middle-man for fetching the collection once you access it. Try debugging and seeing what class is actually returned.