Java8 Collections.sort(有时)不会对JPA返回的列表进行排序

时间:2014-11-08 11:32:27

标签: java sorting jpa eclipselink java-8

Java8在我的JPA EclipseLink 2.5.2环境中继续做一些奇怪的事情。我不得不删除问题https://stackoverflow.com/questions/26806183/java-8-sorting-behaviour 昨天因为在这种情况下的排序受到了奇怪的JPA行为的影响 - 我通过在进行最终排序之前强制执行第一个排序步骤找到了解决方法。

仍然在Java 8中使用JPA Eclipselink 2.5.2,以下代码有时在我的环境中不排序(Linux,MacOSX,都使用build 1.8.0_25-b17)。它在JDK 1.7环境中按预期工作。

public List<Document> getDocumentsByModificationDate() {
    List<Document> docs=this.getDocuments();
    LOGGER.log(Level.INFO,"sorting "+docs.size()+" by modification date");
    Comparator<Document> comparator=new ByModificationComparator();
    Collections.sort(docs,comparator);
    return docs;
}

从JUnit测试调用时,上述功能正常工作。 在生产环境中进行debbuging时,我会得到一个日志条目:

INFORMATION: sorting 34 by modification date

但在TimSort中使用nRemaining&lt;的返回语句命中2 - 所以没有排序。 JPA提供的IndirectList(参见What collections does jpa return?)被认为是空的。

static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
                     T[] work, int workBase, int workLen) {
    assert c != null && a != null && lo >= 0 && lo <= hi && hi <= a.length;

    int nRemaining  = hi - lo;
    if (nRemaining < 2)
        return;  // Arrays of size 0 and 1 are always sorted

此解决方法正确排序:

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

问题:

这是一个JPA Eclipselink错误,或者我在自己的代码中通常可以做些什么?

请注意 - 我无法将软件更改为Java8源代码。当前环境是Java8运行时。

我对此行为感到惊讶 - 测试用例在生产环境中正常运行时出现问题尤其令人讨厌。

https://github.com/WolfgangFahl/JPAJava8Sorting有一个示例项目 它具有与原始问题相当的结构。

它包含一个带有JUnit测试的http://sscce.org/示例,通过调用em.clear()从而分离所有对象并强制使用IndirectList,从而使问题可重现。请参阅下面的JUnit案例以供参考。

急切的抓取:

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

单位案件有效。如果使用FetchType.LAZY或者在JDK 8中省略了fetch类型,则行为可能与JDK 7中的行为不同(我现在必须检查它)。 为什么会这样? 此时我假设需要在列表上指定Eager获取或迭代一次以进行排序,基本上在排序之前手动获取。 还有什么可以做的?

JUnit测试

persistence.xml和pom.xml可以从https://github.com/WolfgangFahl/JPAJava8Sorting获取 测试可以使用MYSQL数据库运行,也可以使用DERBY运行(默认值)

package com.bitplan.java8sorting;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.Table;

import org.eclipse.persistence.indirection.IndirectList;
import org.junit.Test;

/**
 * Testcase for 
 * https://stackoverflow.com/questions/26816650/java8-collections-sort-sometimes-does-not-sort-jpa-returned-lists
 * @author wf
 *
 */
public class TestJPASorting {

  // the number of documents we want to sort
  public static final int NUM_DOCUMENTS = 3;

  // Logger for debug outputs
  protected static Logger LOGGER = Logger.getLogger("com.bitplan.java8sorting");

  /**
   * a classic comparator
   * @author wf
   *
   */
  public static class ByNameComparator implements Comparator<Document> {

    // @Override
    public int compare(Document d1, Document d2) {
      LOGGER.log(Level.INFO,"comparing " + d1.getName() + "<=>" + d2.getName());
      return d1.getName().compareTo(d2.getName());
    }
  }

  // Document Entity - the sort target
  @Entity(name = "Document")
  @Table(name = "document")
  @Access(AccessType.FIELD)
  public static class Document {
    @Id
    String name;

    @ManyToOne
    Folder parentFolder;

    /**
     * @return the name
     */
    public String getName() {
      return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
      this.name = name;
    }
    /**
     * @return the parentFolder
     */
    public Folder getParentFolder() {
      return parentFolder;
    }
    /**
     * @param parentFolder the parentFolder to set
     */
    public void setParentFolder(Folder parentFolder) {
      this.parentFolder = parentFolder;
    }
  }

  // Folder entity - owning entity for documents to be sorted
  @Entity(name = "Folder")
  @Table(name = "folder")
  @Access(AccessType.FIELD)
  public static class Folder {
    @Id
    String name;

    // https://stackoverflow.com/questions/8301820/onetomany-relationship-is-not-working
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parentFolder", fetch=FetchType.EAGER)
    List<Document> documents;

    /**
     * @return the name
     */
    public String getName() {
      return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
      this.name = name;
    }

    /**
     * @return the documents
     */
    public List<Document> getDocuments() {
      return documents;
    }

    /**
     * @param documents the documents to set
     */
    public void setDocuments(List<Document> documents) {
      this.documents = documents;
    }

    /**
     * get the documents of this folder by name
     * 
     * @return a sorted list of documents
     */
    public List<Document> getDocumentsByName() {
      List<Document> docs = this.getDocuments();
      LOGGER.log(Level.INFO, "sorting " + docs.size() + " documents by name");
      if (docs instanceof IndirectList) {
        LOGGER.log(Level.INFO, "The document list is an IndirectList");
      }
      Comparator<Document> comparator = new ByNameComparator();
      // here is the culprit - do or don't we sort correctly here?
      Collections.sort(docs, comparator);
      return docs;
    }

    /**
     * get a folder example (for testing)
     * @return - a test folder with NUM_DOCUMENTS documents
     */
    public static Folder getFolderExample() {
      Folder folder = new Folder();
      folder.setName("testFolder");
      folder.setDocuments(new ArrayList<Document>());
      for (int i=NUM_DOCUMENTS;i>0;i--) {
        Document document=new Document();
        document.setName("test"+i);
        document.setParentFolder(folder);
        folder.getDocuments().add(document);
      }
      return folder;
    }
  }

  /** possible Database configurations
  using generic persistence.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- generic persistence.xml which only specifies a persistence unit name -->
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
      version="2.0">
      <persistence-unit name="com.bitplan.java8sorting" transaction-type="RESOURCE_LOCAL">
        <description>sorting test</description>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes> 
        <properties>
        <!--  set programmatically -->
         </properties>
      </persistence-unit>
    </persistence>
  */
  // in MEMORY database
  public static final JPASettings JPA_DERBY=new JPASettings("Derby","org.apache.derby.jdbc.EmbeddedDriver","jdbc:derby:memory:test-jpa;create=true","APP","APP");
  // MYSQL Database
  //  needs preparation:
  //    create database testsqlstorage;
  //    grant all privileges on testsqlstorage to cm@localhost identified by 'secret';
  public static final JPASettings JPA_MYSQL=new JPASettings("MYSQL","com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/testsqlstorage","cm","secret");

  /**
   * Wrapper class for JPASettings
   * @author wf
   *
   */
  public static class JPASettings {
    String driver;
    String url;
    String user;
    String password;
    String targetDatabase;

    EntityManager entityManager;
    /**
     * @param driver
     * @param url
     * @param user
     * @param password
     * @param targetDatabase
     */
    public JPASettings(String targetDatabase,String driver, String url, String user, String password) {
      this.driver = driver;
      this.url = url;
      this.user = user;
      this.password = password;
      this.targetDatabase = targetDatabase;
    }

    /**
     * get an entitymanager based on my settings
     * @return the EntityManager
     */
    public EntityManager getEntityManager() {
      if (entityManager == null) {
        Map<String, String> jpaProperties = new HashMap<String, String>();
        jpaProperties.put("eclipselink.ddl-generation.output-mode", "both");
        jpaProperties.put("eclipselink.ddl-generation", "drop-and-create-tables");
        jpaProperties.put("eclipselink.target-database", targetDatabase);
        jpaProperties.put("eclipselink.logging.level", "FINE");

        jpaProperties.put("javax.persistence.jdbc.user", user);
        jpaProperties.put("javax.persistence.jdbc.password", password);
        jpaProperties.put("javax.persistence.jdbc.url",url);
        jpaProperties.put("javax.persistence.jdbc.driver",driver);

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
            "com.bitplan.java8sorting", jpaProperties);
        entityManager = emf.createEntityManager();
      }
      return entityManager;
    }
  }

  /**
   * persist the given Folder with the given entityManager
   * @param em - the entityManager
   * @param folderJpa - the folder to persist
   */
  public void persist(EntityManager em, Folder folder) {
    em.getTransaction().begin();
    em.persist(folder);
    em.getTransaction().commit();    
  }

  /**
   * check the sorting - assert that the list has the correct size NUM_DOCUMENTS and that documents
   * are sorted by name assuming test# to be the name of the documents
   * @param sortedDocuments - the documents which should be sorted by name
   */
  public void checkSorting(List<Document> sortedDocuments) {
    assertEquals(NUM_DOCUMENTS,sortedDocuments.size());
    for (int i=1;i<=NUM_DOCUMENTS;i++) {
      Document document=sortedDocuments.get(i-1);
      assertEquals("test"+i,document.getName());
    }
  }

  /**
   * this test case shows that the list of documents retrieved will not be sorted if 
   * JDK8 and lazy fetching is used
   */
  @Test
  public void testSorting() {
    // get a folder with a few documents
    Folder folder=Folder.getFolderExample();
    // get an entitymanager JPA_DERBY=inMemory JPA_MYSQL=Mysql disk database
    EntityManager em=JPA_DERBY.getEntityManager();
    // persist the folder
    persist(em,folder);
    // sort list directly created from memory
    checkSorting(folder.getDocumentsByName());

    // detach entities;
    em.clear();
    // get all folders from database
    String sql="select f from Folder f";
    Query query = em.createQuery(sql);
    @SuppressWarnings("unchecked")
    List<Folder> folders = query.getResultList();
    // there should be exactly one
    assertEquals(1,folders.size());
    // get the first folder
    Folder folderJPA=folders.get(0);
    // sort the documents retrieved
    checkSorting(folderJPA.getDocumentsByName());
  }
}

3 个答案:

答案 0 :(得分:15)

嗯,这是一个完美的教学游戏,告诉你为什么程序员不应该扩展不是为子类设计的类。像“Effective Java”这样的书籍告诉你原因:当超类演变时,试图拦截每个方法来改变它的行为都会失败。

在这里,IndirectList扩展Vector并覆盖几乎所有方法来修改其行为,这是一种明确的反模式。现在,使用Java 8,基类已经发展。

从Java 8开始,接口可以有default个方法,因此添加了sort等方法,这些方法的优点是,与Collections.sort不同,实现可以覆盖方法并提供更多实现适合特定的interface实现。 Vector执行此操作有两个原因:现在所有方法都synchronized的合同也扩展到排序,优化的实现可以将其内部数组传递给Arrays.sort方法,跳过复制操作从以前的实现中已知(ArrayList也是如此)。

即使对于现有代码,也要立即获得此优势,Collections.sort已经过改造。它委托给List.sort,默认情况下委托给另一个方法,该方法通过toArrayTimSort实现复制的旧行为。但是,如果List实施覆盖List.sort,它也会影响Collections.sort的行为。

                  interface method              using internal
                  List.sort                     array w/o copying
Collections.sort ─────────────────> Vector.sort ─────────────────> Arrays.sort

答案 1 :(得分:4)

等待修复错误https://bugs.eclipse.org/bugs/show_bug.cgi?id=446236。 可以使用下面的依赖项或快照。

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

在此之前使用问题中的解决方法:

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

或在可能的情况下指定急切提取:

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

答案 2 :(得分:3)

您遇到的问题不是排序。

通过Arrays.sort调用TimSort,执行以下操作:

TimSort.sort(a, 0, a.length, c, null, 0, 0);

因此,您可以看到TimSort获取的数组大小为0或1。

Arrays.sortCollections.sort调用,执行以下操作。

Object[] a = list.toArray();
Arrays.sort(a, (Comparator)c);

因此,您的集合未被排序的原因是它返回一个空数组。因此,通过返回一个空数组,正在使用的集合不符合集合API。

你说你有一个持久层。所以听起来问题是你正在使用的库以一种懒惰的方式检索实体,并且除非必须,否则不会填充其后备数组。仔细查看您尝试排序的集合,看看它是如何工作的。您的原始单元测试没有显示任何内容,因为它没有尝试对生产中使用的相同集合进行排序。