您喜欢 JPA 2.0 的 Criteria api 吗?你用它与框架吗?
我习惯于在 Hibernate 中使用 Criteria API,并且刚刚观察了 JPA 2.0 中的 Criteria 是如何工作的。
我最喜欢 Hibernate Criteria 的一点是我们可以轻松地使用 Criterion 进行编写。
JPA Criteria 对我来说似乎相当沉重,并且不如 Hibernate Criteria 流畅。 对我来说,与 Hibernate 相比的主要好处之一似乎是使用元模型(Entity_.java 类),但它仍然有点繁重,因为你必须维护 2 个类,或者添加一个注释处理器(尽管它很容易)与 maven 插件集成)
我读过 SpringSource 上的一篇博客,该博客允许降低使用“JPA 谓词”进行组合的复杂性,并且有点类似于 Hibernate 中的 Criterions。
Querydsl 项目看起来很有趣。
http://blog.springsource。 com/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
只是想知道,对于那些使用 JPA2 的人来说,您使用的是 Criteria API 还是对你来说有点太重了吗?
对于那些熟悉 Hibernate Criteria 并现在使用 JPA Criteria 的人来说,您最喜欢哪种方法,为什么?
您是否正在考虑使用 Querydsl 或基于 JPA2 的 Spring 规范之类的框架来构建查询谓词? 您认为这些框架是否足够成熟,可以被采用?
I'm used to work with Criteria API in Hibernate, and just watched how the Criteria in JPA 2.0 work.
What i like most in the Criteria of Hibernate is the ease we have to compose with Criterions.
The JPA Criteria seems quite heavy for me, and not as fluent as the Hibernate Criteria.
One of the major benefits compared to Hibernate for me seems to be the use of the metamodel (Entity_.java classes), but still it's a bit heavy too since you have to maintain 2 classes, or add an annotation processor (though its quite easy to integrate with maven plugins)
I've read a blog on SpringSource that permits to reduce the complexity of composing with "JPA Predicates" and do a bit like with Criterions in Hibernate.
The Querydsl project seems interesting.
http://blog.springsource.com/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
Just wonder, for those using JPA2, are you using Criteria API or it's a bit too heavy for you?
For those familiar with Hibernate Criteria and who now work with JPA Criteria, dowhich approach do you like the most and why?
Are you considering of using, or using, a framework like Querydsl or Spring Specifications over JPA2 for building your query predicates?
Do you think these frameworks are mature enough to be adopted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我只能提供一个有偏见的意见,因为我是 Querydsl 的维护者,但我认为 Querydsl 和 Spring 规范都足够成熟。两者都在 JPA 2 之上提供了一个薄层,Querydsl 为查询构建提供了流畅的 DSL,为 DAO/Repository 构建提供了 Spring Data 模式。
Querydsl 的表达能力与 HQL/JPQL 相当。
以下是一些 JPA 2 Criteria 查询及其 Querydsl 版本:http://source.mysema。 com/forum/mvnforum/viewthread_thread,49
我们在论坛中提供快速的错误修复周期和响应式支持。 Querydsl 团队与 Spring、DataNucleus 等各方合作,改善 Java 中类型安全查询的状态。
I can only provide a biased opinion, since I am the maintainer of Querydsl, but I think that both Querydsl and Spring Specifications are mature enough. Both provide a thin layer on top of JPA 2, Querydsl provides a fluent DSL for query construction and Spring Data patterns for DAO/Repository construction.
The expressivity of Querydsl is comparable to HQL/JPQL.
Here are a few JPA 2 Criteria queries and their Querydsl versions : http://source.mysema.com/forum/mvnforum/viewthread_thread,49
We provide fast bug fix cycles and responsive support in our forums. The Querydsl team cooperates with Spring, DataNucleus and other parties to improve the state of typesafe querying in Java.
不,不喜欢它,因为 API 缺乏可表达性和流畅性。是的,QueryDSL 用更少的代码完成了同样的事情,而且更自然。因此,我们对 JDO 自己的类型安全查询 API 采取了相同的路线,并将其与 JPA Criteria 进行比较
https://datanucleus.wordpress.com/2010 /11/09/jdo-typesafe-vs-jpa-criteria/
No, don't like it, due to the lack of expressability and fluency in the API. Yes QueryDSL does the same in less code, and more naturally. For that reason we've taken the same route for JDO's own typesafe query API, comparing it to JPA Criteria
https://datanucleus.wordpress.com/2010/11/09/jdo-typesafe-vs-jpa-criteria/
我大量使用 JPA 标准构建器。我使用元模型,类型安全的变体,但这不是唯一的选择。如果您想将 JPA 与其他框架进行比较,请不要考虑您需要生成元模型类这一事实。您不必为了相同的功能而只为了额外的好处。不过,我不会回到任何非类型安全的方法。
当你习惯了它之后,你会发现它使用起来非常简单,而且仍然很强大。
另一个好处是:它是标准的。例如:来自不同组织的多位专家都同意这一点。规范的质量是确定的,并且仍在不断发展。并且您可以获得多种实现(实际上,其中至少有两种在最流行的应用程序服务器中使用)。如果你采用一个随机的框架,你就无法得到这一点,它可能经得起时间的考验,也可能经不起时间的考验。
JPA2 中的一些示例:
您可以将所有这些压缩到一行,但我大多选择不这样做,以获得 100% 清晰的代码和每行 1 个语句。
I use JPA criteria builder intensively. I use the metamodel, type-safe variant, but that isn't the only option. If you want to compare JPA to other frameworks, don't count the fact that you need to generate metamodel classes. You don't, for the same functionnality, only for added-on benefits. I wouldn't go back to any non-type-safe approach, though.
When you get used to it, it's quite simple to use, and still powerful.
One added benefit: it's standard. As in: multiple experts from different organizations agreed on it. The quality of the specification is certain, and it's still evolving. And you get multiple implementations (really, at least two of them being used in the most popular application servers). You don't get that if you take a random framework, which may or may not stand the proof of time.
Some examples from JPA2:
You could squeeze all of this to one line, but I mostly choose not to do it to get 100% clear code and 1 statement per line.
我在我的项目中使用 JPA2,并且总是发现 Criteria API 有点过于复杂,特别是对于构建带有一些 where 条件的简单选择语句。
所以我决定继续使用 JPQL 查询。也许 Criteria API 在处理更复杂的查询方面有其优势(除了一些其他优点之外)。
I use JPA2 in my projects and always found Criteria API a bit overcomplicated especially for building simple select statements with a few where conditions.
So I decided to go on using JPQL queries. Maybe the Criteria API has its strength with more complex queries (besides some other advantages).