getResultList()hibernate的默认排序顺序 [英] Default sorting order of getResultList() hibernate

查看:637
本文介绍了getResultList()hibernate的默认排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hibernate用于实现JPA的getResultList()的排序机制是什么?它是由id还是不能保证一个正确的顺序?我没有在JPA javadoc中看到有关这方面的任何细节。

  / ** 
*执行SELECT查询并返回查询结果
*作为一个无类型的列表。
*
* @return返回结果列表
*
* @throws IllegalStateException如果调用Java
*持久性查询语言UPDATE或DELETE语句$ b $如果查询执行超过
*查询超时值集并且只有语句是
*回滚
* @throws TransactionRequiredException如果锁定模式具有
*,则b * @rowrows QueryTimeoutException如果悲观锁定
*失败并且事务回滚
* @throws LockTimeoutException,如果悲观锁定
*失败并且仅存在事务
* @throws PessimisticLockException语句回滚
* @throws PersistenceException如果查询执行超过
*查询超时值集并且事务
*被回滚
* /
List getResultList ();

但是,每次运行并测试结果时,都会给出按id排序的列表。这是我仍然困惑,虽然天才已经投下了这个问题



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

解决方案

这是数据库返回的元素的顺序。 b
$ b

由于数据库可以以任意顺序返回 ResultSet ,所以您永远不会知道它的顺序(特别是在插入和删除in一个表)。



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


What is the sorting mechanism of Hibernate for implementation of getResultList() of JPA ? Is it by the id or is it not guaranteed a proper order ? I did not see any detail regarding this in 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();

But each time I run and test the results it give me the list ordered by id. That is what I am still confused although a genius had down voted this problem

I just put show_sql true and checked the generated sql. It does not have any sorting included.

解决方案

It's is the order of the elements returned by the database.

You will never know which order it is as the database is free to return a ResultSet in any order (especially when inserting and deleting in a table).

To ensure a certain order you have to define this yourself.

这篇关于getResultList()hibernate的默认排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆