使用方法:JPQL或Criteria API? [英] What to use: JPQL or Criteria API?

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

问题描述

我的Java应用程序正在使用JPA进行对象持久化。业务域非常简单(只有三个类是持久的,每个类有3-5个属性)。查询也很简单。问题是我应该使用哪种方法:JPQL或Criteria API?

My Java application is using JPA for object persistence. The business domain is very simple (just three classes are persistent, with 3-5 properties in each). Queries are simple as well. The question is which approach I should use: JPQL or Criteria API?

推荐答案

我很确定这已经涵盖了在SO,但我找不到现有的问题。所以,这是我对这个问题的观点:

I'm pretty sure this has already been covered here on SO but I couldn't find the existing question. So, here is my point of view on the question:


  • 我发现JPQL查询更容易写/读。

  • 我发现Criteria API非常适合构建动态查询。

这基本上就是你的将在 Hibernate:Criteria vs. HQL 中找到。

Which is basically what you'll find in Hibernate: Criteria vs. HQL.

但JPA 2.0 Criteria API和值得一提的Hibernate Criteria API之间存在一个主要区别:JPA 2.0 Criteria API是 typesafe API 因此提供了编译时间检查,代码完成,更好的重构支持等。
但是,并没有发现这些好处超过了JPQL的易用性。

But there is one major difference between the JPA 2.0 Criteria API and the Hibernate's Criteria API that is worth mentioning: the JPA 2.0 Criteria API is a typesafe API and thus gives compile time checks, code completion, better refactoring support, etc. However, I don't find that the benefits outweighs the ease of use of JPQL.

总之,除了动态查询(例如多标准搜索功能)之外,我更青睐JPQL。

To sum up, I would favor JPQL, except for dynamic queries (e.g. for multi criteria search features).

  • Hibernate: Criteria vs. HQL
  • What are some of the real world example where JPA2 Criteria API is more preferable?
  • Hibernate Querying 102 : Criteria API

这篇关于使用方法:JPQL或Criteria API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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