gpt4 book ai didi

java - hibernate getResultList() 的默认排序顺序

转载 作者:行者123 更新时间:2023-12-01 07:30:55 26 4
gpt4 key购买 nike

Hibernate 实现 JPA 的 getResultList() 的排序机制是什么?是通过 id 还是不能保证正确的顺序?我在 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返回DB返回的默认顺序。但是,您可以在查询中指定顺序

    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();

关于java - hibernate getResultList() 的默认排序顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17607755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com