Java getResultList()休眠的默认排序顺序

Java getResultList()休眠的默认排序顺序,java,hibernate,sorting,jpa,Java,Hibernate,Sorting,Jpa,Hibernate实现JPA的getResultList的排序机制是什么?是凭身份证还是不能保证订单正确?我在JPA javadoc中没有看到任何关于这方面的细节 /** * Execute a SELECT query and return the query results * as an untyped List. * * @return a list of the results * * @throws IllegalState

Hibernate实现JPA的getResultList的排序机制是什么?是凭身份证还是不能保证订单正确?我在JPA javadoc中没有看到任何关于这方面的细节

/**
     * Execute a SELECT query and return the query results
     * as an untyped List.
     *
     * @return a list of the results
     *
     * @throws IllegalStateException if called for a Java
     * Persistence query language UPDATE or DELETE statement
     * @throws QueryTimeoutException if the query execution exceeds
     * the query timeout value set and only the statement is
     * rolled back
     * @throws TransactionRequiredException if a lock mode has
     * been set and there is no transaction
     * @throws PessimisticLockException if pessimistic locking
     * fails and the transaction is rolled back
     * @throws LockTimeoutException if pessimistic locking
     * fails and only the statement is rolled back
     * @throws PersistenceException if the query execution exceeds
     * the query timeout value set and the transaction
     * is rolled back
     */
    List getResultList();
但每次我运行并测试结果时,它都会给我按id排序的列表。这就是我仍然感到困惑的地方,尽管一个天才否决了这个问题


我只是将show_sql设置为true并检查生成的sql。它没有包含任何排序。

它是数据库返回的元素的顺序

您永远不会知道它的顺序,因为数据库可以自由地以任何顺序返回结果集,尤其是在表中插入和删除时

为了确保某种顺序,您必须自己定义它。

javax.persistence.Query中的getResultList方法返回数据库返回的默认顺序。但是,您可以在查询中指定顺序

    List customerList = em.createQuery("SELECT r FROM Customer r").getResultList();
    List customerList1 = em.createQuery("SELECT r FROM Customer r order by r.lastUpdatedDate").getResultList();
    List customerList2 = em.createQuery("SELECT r FROM Customer r order by r.lastUpdatedDate desc").getResultList();

是的,我也觉得你是对的。但当我测试时,我总是按照id对这些结果进行排序:O@SanjayaLiyanage您可能已经插入了所有具有升序id的数据。那么,新表以这种顺序返回数据是很自然的,但正如我所说的,您不能指望这一点。那么,这是我们插入数据的顺序吗?Chase也这么认为吗?对不起,我问这些事情是为了确保answer@SanjayaLiyanage如果从未删除,则在大多数情况下是插入数据的顺序。删除数据时,可能会在删除数据的位置插入新数据。因此,现在您将永远不会看到,也没有任何dataase对顺序做出任何规定。因此,如果我没有弄错的话,这取决于特定的DB MySQL、Oracle、Derby如何提供数据