Hibernate Criteria Limit 机制?

发布于 2024-12-02 12:36:34 字数 306 浏览 0 评论 0原文

Hibernate Criteria 支持提供了 setMaxResults() 方法来限制从数据库返回的结果。

我在他们的文档中找不到任何答案 - 这是如何实现的?是查询整个结果集然后只返回请求号吗?或者它真的限制了数据库端的查询(想想 mySql 中的 LIMIT 关键字)。

这很重要,因为如果查询可能返回很多结果,我确实需要知道 setMaxResults() 是否仍会查询数据库中的所有行(这会很糟糕)。

另外-如果它真正限制了数据库端的行数,那么它如何实现这种跨数据库(因为我不认为每个rdbms都支持像mySql那样的LIMIT功能)。

Hibernate Criteria support provides a setMaxResults() method to limit the results returned from the db.

I can't find any answer to this in their documentation - how is this implemented? Is it querying for the entire result set and then returning only the request number? Or is it truly limiting the query on the database end (think LIMIT keyword as in mySql).

This is important because if a query could potentially return many many results, I really need to know if the setMaxResults() will still query for all the rows in the database (which would be bad).

Also - if its truly limiting the number of rows on the database end, how is it achieving this cross-db (since I don't think every rdbms supports a LIMIT functionality like mySql does).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄意 2024-12-09 12:36:34

Hibernate 要求数据库限制查询返回的结果。它通过方言来完成此操作,方言使用任何特定于数据库的机制来执行此操作(因此对于 SQL Server,它将执行“从表中选择顶部 n *”之类的操作,Oracle 将执行“从表中选择 *,其中 rownum <<”)。 n”,MySQL 将执行“select * from table limit n”等)。然后它只返回数据库返回的内容。

Hibernate asks the database to limit the results returned by the query. It does this via the dialect, which uses whatever database-specific mechanism there is to do this (so for SQL Server it will do somthing like "select top n * from table", Oracle will do "select * from table where rownum < n", MySQL will do "select * from table limit n" etc). Then it just returns what the database returns.

等待圉鍢 2024-12-09 12:36:34

org.hibernate.dialect.Dialect 类包含一个名为supportsLimit() 的方法。如果方言子类重写此方法,它们可以以其数据库风格原生的方式实现行限制处理。您可以看到从类 org 中调用此代码的位置.hibernate.loader.Loader 有一个名为 prepareQueryStatement 的方法,只需搜索单词 limit 即可。

但是,如果方言不支持此功能,则会对 ResultSet 迭代器进行严格检查,以确保在达到限制时停止构造 Java 对象(实体)结果。此代码也位于 Loader 中。

The class org.hibernate.dialect.Dialect contains a method called supportsLimit(). If dialect subclasses override this method, they can implement row limit handling in a fashion native to their database flavor. You can see where this code is called from in the class org.hibernate.loader.Loader which has a method titled prepareQueryStatement, just search for the word limit.

However, if the dialect does not support this feature, there is a hard check in place against the ResultSet iterator that ensures Java object (entity) results will stop being constructed when the limit is reached. This code is also located in Loader as well.

执妄 2024-12-09 12:36:34

我同时使用 Hibernate 和 Hibernate Search,在不查看底层实现的情况下,我可以告诉您它们肯定不会返回所有结果。我已经实现了返回所有结果的相同查询,然后将其更改为设置第一个结果和最大结果(以实现分页),并且性能提升是巨大的。

他们可能为此使用方言特定的 SQL,例如 MySQL 中的 LIMIT、Oracle 中的 ROWNUM。您的实体管理器知道您正在使用的方言,因此这很简单。

最后,如果您确实想检查 Hibernate 为该查询生成了什么 SQL,只需在创建实体管理器/工厂时将“show_sql”属性设置为 true,它就会将正在运行的所有 SQL 吐出到控制台。

I use both Hibernate and Hibernate Search and without looking at the underlying implementation I can tell you that they definitely do not return all results. I have implemented the same query returning all results and then changed it to set the first result and max results (to implement pagination) and the performance gains were massive.

They likely use dialect specific SQL for this, e.g. LIMIT in MySQL, ROWNUM in Oracle. Your entity manager is aware of the dialect that you are using so this is simple.

Lastly if you really want to check what SQL Hibernate is producing for this query, just set the "show_sql" property to true when you create your entity manager / factory and it spits out all the SQL it is running to the console.

格子衫的從容 2024-12-09 12:36:34

HQL 不支持像 SQL 中那样的查询内部限制,仅支持您也发现的 setMaxResults()

要查明它是否将 setMaxResults() 转换为 LIMIT 查询,您可以打开 SQL 日志记录。

HQL does not suppport a limitation inside a query like in SQL, only the setMaxResults() which you also found.

To find out if it transform the setMaxResults() into a LIMIT query, you can turn on your SQL logging.

风和你 2024-12-09 12:36:34

我知道问题有点老了。但是,setMaxResults() 确实限制了数据库端的行数。

如果您仔细查看 Hibernate SQL 输出,您会发现以下 SQL 语句已附加到您的查询中。

限制?

I know Question is bit old. But yes setMaxResults() is truly limiting the number of rows on the database end.

If you really look into your Hibernate SQL output, you can find the following SQL statement has been appended to your query.

limit ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文