Hibernate 5和Typed Criteria Queries(JPA2) [英] Hibernate 5 and Typed Criteria Queries (JPA2)

查看:109
本文介绍了Hibernate 5和Typed Criteria Queries(JPA2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:1)如果我从Hibernate 4.x升级到Hibernate 5.x,我仍然可以使用旧Criteria Queries,或者只使用新的键入 JPA2标准查询?



2)我是否正确理解新的输入条件迫使我创建第二个类为<每个实体类都有,因此复制了多少个类?我应该手动创建这些类吗?如果没有,如何? Rant:不得不复制课程看起来很奇怪,所以我必须以某种方式误解它?这不是过度杀伤和不必要的复杂吗?

解决方案


  1. 不,旧标准API不会被弃用。只需查看 javadoc :没有弃用警告。但我建议坚持使用标准的JPA API,而不是使用标准的JPA API与专有的Hibernate API混合。


  2. 不,它不强制您。您仍然可以使用字符串标识符。但是,如果类型安全是目标,则强烈建议使用元模型类。

当然,您不需要手工生成这些内容。它们由您的实体类中的注释处理器生成,他们的映射注释。您可能会发现它很奇怪,但使用 root.get(MyEntity_.firstName) root.get(firstName)更安全。 code>:在编译时检测到错字,并且如果将该字段重构为 firstname ,编译器将生成一个错误,而不是让您使用旧的firstName字符串标识符。

我仍然发现JPQL查询的编写,理解和维护要简单得多,并且只能在基于多个生成动态查询时使用标准API。标准。使用自动化测试来检查查询是否正确。做同样的事情,即使是标准查询,顺便说一句。

Questions:

1) If I upgrade from Hibernate 4.x to Hibernate 5.x, can I still use the "old" Criteria Queries, or only the new Typed JPA2 Criteria Queries? Is the old one deprecated, or can I use both side by side?

2) Did I understand correctly that the new Typed Criteria forces me to create a second class for each Entity class I have, thus duplicating the number of classes? Am I supposed to create these classes by hand? If not, how? Rant: Having to duplicate classes seem bizarre, so I must be misunderstanding it somehow? Isn't that overkill and unnecessarily complicated?

解决方案

  1. No, the old criteria API is not deprecated. Just look at the javadoc: there is no deprecation warning. But I would recommend sticking to the standard JPA API, and not using the standard JPA API mixed with the proprietary Hibernate API.

  2. No, it doesn't force you. You can still use String identifiers. But if type-safety is the goal, using the metamodel classes is strongly recommended.

Of course you don't need to generate these by hand. They're generated by an annotation processor, from your entity classes and their mapping annotations. You may find it bizarre, but it's much safer to use root.get(MyEntity_.firstName) than root.get("firstName"): a typo is detected at compilation time, and if you refactor the field to firstname, the compiler will generate an error instead of letting you use the old "firstName" string identifier.

I still find JPQL queries much simpler to write, understand and maintain though, and would only use the criteria API when a dynamic query has to be generated based on multiple... criteria. Use automated tests to check if the queries are correct. Do the same, even with the criteria queries, BTW.

这篇关于Hibernate 5和Typed Criteria Queries(JPA2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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