Java8 Collections.sort (sometimes) does not sort JPA returned lists

后端 未结 3 702
攒了一身酷
攒了一身酷 2020-11-28 12:24

Java8 keeps doing strange things in my JPA EclipseLink 2.5.2 environment. I had to delete the question https://stackoverflow.com/questions/26806183/java-8-sorting-behaviour

3条回答
  •  再見小時候
    2020-11-28 12:39

    Wait for the bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=446236 to be fixed. Use the below dependency when it get's available or a snapshot.

    
      org.eclipse.persistence
      eclipselink
      2.6.0
    
    

    Until then use the workaround from the question:

    if (docs instanceof IndirectList) {
        IndirectList iList = (IndirectList)docs;
        Object sortTargetObject = iList.getDelegateObject();
        if (sortTargetObject instanceof List) {
            List sortTarget=(List) sortTargetObject;
            Collections.sort(sortTarget,comparator);
        }
    } else {
        Collections.sort(docs,comparator);
    }
    

    or specify eager fetching where possible:

    // http://stackoverflow.com/questions/8301820/onetomany-relationship-is-not-working
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parentFolder", fetch=FetchType.EAGER)
    

提交回复
热议问题