JPA为什么使用createNamedQuery [英] JPA why use createNamedQuery

查看:155
本文介绍了JPA为什么使用createNamedQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate API将DAO层改为使用纯JPA API实现。看起来推荐的方法是使用实​​体管理器中的createNamedQuery。指定的查询存储在模型/实体类的注释中。这对我来说没有意义。为什么要在模型对象中定义JPA查询,但在DAO中使用它们。从DAO本身中使用createQuery并定义DAO中的查询,甚至只是在DAO本身中定义命名的查询,是不是更有意义?



对于那些使用JPA API实现DAO层的人,你是如何定义查询的?



这样做有两个原因:


  1. 它将它们放入一个更中心的地方,而不是散布在随机createQuery()调用的代码中;和
  2. 构建过程可以验证这些查询(真的很有用)。


I'm in the processes of changing my DAO layer from using Hibernate API to using a pure JPA API implementation. It looks like the recommended method is to use the createNamedQuery from the entity manager. The named queries are stored in annotations in the model/entity classes. This just not makes sense to me. Why would you define the JPA Queries in the model objects but use them in the DAOs. Wouldn't it make more sense to just use createQuery from within the DAO itself and define the queries in the DAO or even just define the named queries in the DAO itself?

For those of you that have implemented your DAO layer using the JPA API how have you defined your queries?

解决方案

I use named queries.

There are two reasons to do so:

  1. It puts them in a more central place rather than scattered in code with random createQuery() calls; and
  2. Build processes can validate the queries (really useful).

这篇关于JPA为什么使用createNamedQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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