Hibernate - 标准与命名查询 [英] Hibernate -- Criteria vs named query

查看:119
本文介绍了Hibernate - 标准与命名查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图比较 Hibernate Criteria 到性能的命名查询。我知道这一切都取决于实际查询本身,最后一句话是关于他们如何在运行时进行配置。尽管如此,仍然在努力理清每一种情况。

我试图把Q分成两部分,寻找验证/更正:



PART-1 - Hibernate Criteria和命名查询的基本工作原理:



标准适用于参数。
在运行时,查询不需要解析 - 有几个搜索和
这样的形式上呈现参数可以对结果进行排序,将它们返回为可滚动等。
没有阅读/验证了这一点,但Criteria在字段(基于它们设置的参数)
使用
索引来加快速度。

因此,与简单HQL
相比,Criteria的优势在于执行期间的速度。

命名查询与HQL--
相比具有相同的优势,查询在启动时被解析一次。然后在应用程序中从必要的地方执行


PART-2 - 比较两种: b
$ b



Criteria和命名查询如何相互比较?



Criteria适用于复杂查询多个表
和多个参数 - 有优化的方法,从而使查询更快(?)



命名查询具有define-一次性使用和
对于轻量级查询来说相当不错 - 通常在单个表上使用少量参数的复杂搜索少于
。甚至在频繁查询时更好。


注意:看到非常有用的 Hibernate Criteria vs HQL:哪个更快?在其他一些讨论中。

TIA。

解决方案

您不会根据性能选择一个。最后,它无论如何都会成为SQL查询,重要的是SQL查询的性能。

执行SQL查询的速度大于解析HQL查询并将其转换为SQL的速度。因此,即使您不使用命名查询,性能也不会太差。



基于功能和可读性,您选择HQL的标准。

如果您想要可读的内容,请使用HQL查询。

如果您想基于各种可选的搜索条件动态组合查询,那么Criteria API允许这样做,并且比动态组合HQL查询更方便。


I'm trying to compare Hibernate Criteria to named queries for performance. i'm aware it all depends on the actual query itself and the last word is on how they profile in runtime. still, trying to sort out what goes into each.

i tried to organize the Q in two parts & looking for verification/correction on both:

PART-1 -- how Hibernate Criteria and named queries work basically:

Criteria works on parameters. In runtime, the query doesn`t need parsing-- has several search and "present-in-form" params like order the results, return them as scrollable etc. Haven't read/verified this, but Criteria works by indexes on fields (based on the parameters set on them) to make it all faster.

So, the advantage of Criteria compared to plain HQL is its speed during execution.

Named queries have the same advantage over HQL-- the query is parsed once at start-up. Then executed from wherever necessary in the application.

PART-2 -- comparing the two:

So in this picture,

how do Criteria and named-queries compare to one another?

Criteria works well for complicated queries across multiple tables and multiple params-- has the means to optimize and thus make the query fast(?)

named-queries have the advantage of "define-once-use-everywhere" and are quite alright for "light" queries-- less complicated searches with small amount of params typically on a single table. even better on frequent queries.

Note: seen the very useful Hibernate Criteria vs HQL: which is faster? among some other discussions.

TIA.

解决方案

You don't choose one over the other based on performance. In the end, it becomes a SQL query anyway, and what matters is the performance of the SQL query.

Executing a SQL query is orders of magnitudes slower than parsing a HQL query and transforming it to SQL. So even if you don't use a named query, the performance won't be significantly worse.

You choose criteria over HQL based on capabilities and readability.

If you want something readable, use a HQL query.

If you want to compose a query dynamically, based on various, optional, search criteria, then the Criteria API allows that, and is more convenient than dynamically composing an HQL query.

这篇关于Hibernate - 标准与命名查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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