JPA为什么使用createNamedQuery

发布于 2024-07-19 06:04:11 字数 280 浏览 6 评论 0原文

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

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

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?

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

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

发布评论

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

评论(5

赤濁 2024-07-26 06:04:12

拥抱 和 的力量:) 如果您有一个有意义的查询可以放入模型中,请这样做。 如果你不这样做,就不要这样做。
最好问“你为什么用 JPS 编写 DAO?” 如果答案是“将我的代码与数据库隔离”。 这是由实现 JPA 的库完成的。 如果答案是“将我的代码与我持久化事物的方式的更改隔离开来”,那么 JPA 通过允许您拥有不同的实现来实现这一点。
我使用查询对象进行复杂查询,并且在可能的情况下仍然使用命名查询,并且我喜欢命名查询被编译的事实,因此我可以更快地找到其中的错误。 我没有 DAO 层。

Embrace the power of and :) If you have a query that makes sense to put into the model, do so. If you don't, don't.
It might be even better to ask "Why are you writing DAO's with JPS?" If the answer is to "Isolate my code from the database." This is done by the library implementing JPA. If the answer is "to isolate my code from changes to the way I persist things", then JPA does that by allowing you to have different implementations.
I use query objects for complex queries, and still used named queries where possible, and I like the fact that named queries get compiled, and so I find bugs in them that much faster. I have no DAO layer.

风吹雨成花 2024-07-26 06:04:11

我使用命名查询。

这样做有两个原因:

  1. 它将它们放在更中心的位置,而不是通过随机 createQuery() 调用分散在代码中; 构建
  2. 过程可以验证查询(非常有用)。

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).
各自安好 2024-07-26 06:04:11

您可以查看 Spring Data JPA。 它允许您简单地定义一个接口并执行查询,而无需手动实现执行。

实体:

@Entity
@NamedQuery(id="User.findByLastname" query="from User u where u.lastname = ?1")
public class User implements Persistable<Long> {

  @Id
  private Long id;
  private String username;
  private String lastname;
  private int age;
}

存储库:

public interface UserRepository extends CrudRepository<User, Long> {

  // Will trigger the NamedQuery due to a naming convention
  List<User> findByLastname(String lastname);

  // Will create a query from the methodname
  // from User u where u.username = ?
  User findByUsername(String username);

  // Uses query annotated to the finder method in case you
  // don't want to pollute entity with query info
  @Query("from User u where u.age > ?1")
  List<User> findByAgeGreaterThan(int age);
}

设置:

EntityManager em = Persistence.getEntityManagerFactory().createEntityManager();
JpaRepositoryFactory factory = new JpaRepositoryFactory(em);

UserRepository repository = factory.getRepository(UserRepository.class);

如您所见,您可以选择不同的方法来派生要从方法执行的查询。 虽然直接从方法名称派生它对于简单查询来说是可行的,但您可能会在 @NamedQuery (JPA 标准)或 @Query (Spring Data JPA 注释)依赖之间进行选择取决于您有多喜欢遵守标准。

Spring Data JPA 为您提供数据访问层实现的各个其他方面的支持,允许提供方法的自定义实现,并与 Spring 很好地集成。

You could take a look at Spring Data JPA. It allows you to simply define an interface and execute queries without the need to implement the execution manually.

Entity:

@Entity
@NamedQuery(id="User.findByLastname" query="from User u where u.lastname = ?1")
public class User implements Persistable<Long> {

  @Id
  private Long id;
  private String username;
  private String lastname;
  private int age;
}

Repository:

public interface UserRepository extends CrudRepository<User, Long> {

  // Will trigger the NamedQuery due to a naming convention
  List<User> findByLastname(String lastname);

  // Will create a query from the methodname
  // from User u where u.username = ?
  User findByUsername(String username);

  // Uses query annotated to the finder method in case you
  // don't want to pollute entity with query info
  @Query("from User u where u.age > ?1")
  List<User> findByAgeGreaterThan(int age);
}

Setup:

EntityManager em = Persistence.getEntityManagerFactory().createEntityManager();
JpaRepositoryFactory factory = new JpaRepositoryFactory(em);

UserRepository repository = factory.getRepository(UserRepository.class);

As you see you can choose between different ways to derive the query to be executed from the method. While deriving it directly from the method name is feasible for simple queries you'd probably choose between the @NamedQuery (JPA standard) or @Query (Spring Data JPA annotation) dependening on how much you like to stick with standards.

Spring Data JPA gives you support in various other corners of data access layer implementation, allows providing custom implementations for methods and nicely integrates with Spring.

寄居者 2024-07-26 06:04:11

我的经历与 cletus 完全相反——我没有发现任何好处,而且发现它们使用起来很尴尬。 简单的查询在哪里定义没有什么区别,但非简单的查询通常很难与任何单个实体关联,但使用面向业务的方法很容易。

如果您在 DAO 中使用或多或少复杂的基础设施(为了重用和一致性),那么命名查询的使用往往会使实现和可读性变得复杂,并且没有提供明显的好处。

通过构建过程验证查询听起来很有趣 - 我想更多地了解它的真正含义...我的查询几乎没有留下任何错误余地,因为每个 DAO 方法都经过单元测试,尽可能有意义。

I had experience completely opposite to the one of cletus - I found no benefit and also found them awkward to use. Trivial queries would make no difference where to define, but non-trivial queries are usually hard to associate with any single entity but easy with business-oriented method.

If you use more or less sophisticated infrastructure in DAOs (for re-use and consistency) then usage of named queries tend to complicate both implementation and readability with no apparent benefit to offer.

Validating of queries by build process sounds interesting - I would like to know more what it really means... My queries leave little margin for error since every DAO method is unit tested as much as it makes sense.

土豪我们做朋友吧 2024-07-26 06:04:11

不要到处使用命名查询,在某些情况下它们是不合适的,例如当它很少使用的查询时,那么在根据需要构建时它可能会更有效。

当命名查询变得复杂并且频繁执行时,它更有意义。

[已更新]

您可以在映射时编写命名查询。

Don't just use named queries everywhere, there are cases where they are not appropriate, such as when its gonna be rarely used queries then it may be more efficient when built on as needed basis.

Named queries make more sense when its gonna be complex and got executed frequently.

[Updated]

You can write the named queries at mapping, instead.

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