是否仍推荐旧版标准 API?

Is legacy Criteria API still recommended?

我的团队在 Hibernate 3.x 下使用 hibernate Criteria API 已经很长时间了。最近想把Hibernate版本升级到5,但是Criteria API好像不推荐了。但是 JPA 看起来完全不同 API,我们不想冒险。我们应该继续使用旧标准吗?

嗯,Version 5.0 Criteria Chapter 中的警告非常大:

Hibernate offers an older, legacy org.hibernate.Criteria API which should be considered deprecated. No feature development will target those APIs. Eventually, Hibernate-specific criteria features will be ported as extensions to the JPA javax.persistence.criteria.CriteriaQuery. For details on the org.hibernate.Criteria API, see Legacy Hibernate Criteria Queries.

还有更多:

Users of the older Hibernate org.hibernate.Criteria query API will recognize the general approach, though we believe the JPA API to be superior as it represents a clean look at the lessons learned from that API.

所以,这似乎真的取决于您的团队。我花了一段时间才习惯 JPA API,但现在很顺利。我已经回到 Hibernate 以获得非常大的提取性能,但通常我使用任何 JPA 都没有任何问题。我倾向于使用 @NamedQueries 进行基本访问,如果我需要更动态的东西,则使用 Criteria API。

编辑:更新我能够解决性能问题并且应用程序现在是严格的 JPA。

如果由于时间 constraints/project deadlines/other 风险的原因,您无法将代码从遗留的 Hibernate 标准 API 移动到 JPA API,您应该至少设置某种时间表以返回它。 Hibernate 很可能会在接下来的 2 个主要版本中完全删除已弃用的标准 API。