Hibernate命名查询及其性能优势? [英] Hibernate named queries and its performance advantage?

查看:123
本文介绍了Hibernate命名查询及其性能优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hibernate文档说,命名查询的目的是将HQL从项目中的不同位置清除到某个xml中的单个位置(在声明式方法的情况下)。这意味着不需要重新编译n在查询修改的情况下重新加载会话工厂是必需的,这意味着服务器在大多数情况下启动,因为查询对象被缓存。但在注解的情况下,我需要在实体级别定义命名查询。所以这里再次编译需要。我的问题在于命名查询是否有助于提高性能。这是我的理解: -
$ b $ 1)当我使用命名查询时,查询对象被缓存在二级缓存中。当我说只是查询对象,这意味着只是查询语法被缓存而不是查询results.Right?如果它的权利可能只在HQL的情况下才有用,因为我们可以避免将HQL转换为本地查询,每次查询被触发并具有一些良好的时间表现。



但是,如果我们使用的是原生sql,那么命名查询不会提供这种优势,因为在这种情况下没有翻译。



因此,命名查询的主要优点是创建sqls的中央存储库。是的,如果使用HQL,它可以为我们节省一些本地sql的翻译时间,但请记住,查询对象将存在于jvm的生命周期中,并会消耗一些内存。所以一些交易在这里。

解决方案

命名查询有两个小优点:$ b​​
$ b


  • 创建会话工厂时会检查它们的语法,以便在发生错误时应用程序快速失败(这可能表示您的应用程序缺少一些单元测试)
  • 他们可以在几个地方被访问和使用(无论如何,这可能表示设计问题)



他们也有缺点:当使用命名查询读取或调试代码时,无法立即查看正在执行的查询,而无法搜索其定义。 其余部分实际上并不重要: / p>


  • 与实际执行查询的成本相比,将HQL查询转换为SQL的成本可以忽略不计

  • 缓存查询的内存成本非常小,请记住,Hibernate需要将所有实体元数据放在内存中。



我倾向于更喜欢在代码中使用它定义查询,并对它们进行单元测试。这使得代码更具可读性,更强大。

As hibernate document says that purpose of named query is to clear the HQL from different places in project to single place in some xml (in case of declarative approach).This means no recompilation is required n case of query modification but reloading of session factory is required which means server start up in most of the cases as query object is cached. But in case of annotation i need to define the named queries at entity level. So here compilation again requires. My question here is does named query help in performance also. Here is my understanding:-

1)when i use named queries, just query object is cached in second level cache. When i say just query object it means just query syntax is cached not the query results.Right? If its right then probably its useful only in case of HQL becoz thats where we can avoid traslation of HQL to native query ,every time query is fired and have some good time performance.

But if we are using native sql, named query does not provide that advantage as no translation happens in that case.

So main advantage of named query is to make central repository of sqls. Yes in case of HQL it can save us some translation time to native sql also but keep in mind that query object will lie for the life time of jvm and will consume some memory. So some trade off here.

解决方案

Named queries have two small advantages:

  • their syntax is checked when the session factory is created, making the application fail fast in case of an error (which probably indicates that your application lacks some unit tests)
  • they can be accessed and used from several places (which probably indicates a design problem anyway)

They also have a drawback: when reading or debugging code using a named query, you can"t immediately see which query is being executed without searching for its definition.

The rest is really unimportant:

  • the cost of transforming a HQL query to SQL is negligible compared to the cost of actually executing the query
  • the memory cost of caching the query is really small. Remember that Hibernate needs to have all the entities meta-data in memory anyway.

I tend to prefer defining the query in the code where it's used, and to unit test them. That makes the code more readable, and more robust.

这篇关于Hibernate命名查询及其性能优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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