Hibernate命名HQL查询(在注释中)是否优化? [英] Are Hibernate named HQL queries (in annotations) optimised?

查看:181
本文介绍了Hibernate命名HQL查询(在注释中)是否优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一位新同事刚刚建议在Hibernate中使用带有注释的命名HQL查询(即@NamedQuery),而不是在我们的XxxxRepository类中嵌入HQL。

A new colleague has just suggested using named HQL queries in Hibernate with annotations (i.e. @NamedQuery) instead of embedding HQL in our XxxxRepository classes.

我想知道的是,除了集中查询外,使用注释是否提供了任何优势?

What I'd like to know is whether using the annotation provides any advantage except for centralising queries?

特别是,是否有一些性能增益,例如,因为只有在加载类时才会解析查询,而不是每次执行Repository方法时都会解析查询?

In particular, is there some performances gain, for instance because the query is only parsed once when the class is loaded rather than every time the Repository method is executed?

推荐答案


$ b

from Pro EJB 3 (Mike Keith):


...我们建议尽可能使用命名查询。持久性提供程序通常会采取步骤将JPQL命名查询预编译为SQL,作为应用程序部署或初始化阶段的一部分。

"...we recommend named queries whenever possible. Persistence providers will often take steps to precompile JPQL named queries to SQL as part of the deployment or initialization phase of an application."

这本书是关于JPA的,一般的建议适用于Hibernate。但是,由于Hibernate缓存了已解析的HQL / JPQL,因此您可能看不到很大的性能提升。使用命名查询的另一个优势是,它使您可以选择在部署时使用映射文件覆盖查询,而无需重新构建应用程序;如果你需要调整生产中的查询,这很有用。

Although this book is on JPA, the general advice applies to Hibernate. However, since Hibernate caches parsed HQL/JPQL, you probably won't see a big performance gain. Another advantage of using named queries is that it gives you the option to override a query using a mapping file at deployment time without having to rebuild your application; useful if you need to tweak a query in production.

这篇关于Hibernate命名HQL查询(在注释中)是否优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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