39

As we already know, criterion query is deprecated in Hibernate 5. It was such a useful feature in the previous versions of Hibernate. And it still performs better than HQL.

So what is the reason of it's deprecation in Hibernate 5 ?

And also this question is not a duplicate of this question as I want to know the reason of the deprecation of criteria query.

This is from here.

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.

Community
  • 1
  • 1
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • Possible duplicate of [Hibernate 5 and Typed Criteria Queries (JPA2)](http://stackoverflow.com/questions/35824417/hibernate-5-and-typed-criteria-queries-jpa2) – Mick Mnemonic Jul 19 '16 at 01:33
  • From the [docs](https://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/apb.html): _"New development should focus on the JPA `javax.persistence.criteria.CriteriaQuery` API. Eventually, Hibernate-specific criteria features will be ported as extensions to the JPA `javax.persistence.criteria.CriteriaQuery`."_ Also, if you want to write legible queries (instead of using the clunky Criteria API) , have a look at [QueryDSL](http://www.querydsl.com/). – Mick Mnemonic Jul 19 '16 at 01:38
  • @MickMnemonic Not quite duplicate. I edited my question. – Pritam Banerjee Jul 19 '16 at 01:38
  • 2
    I think the part of the documentation that you've quoted is quite self-explanatory: They no longer want to develop Hibernate-specific stuff, but instead encourage users to code against the interface (JPA). The existing features can still be used in Hibernate 5. – Mick Mnemonic Jul 19 '16 at 01:44

1 Answers1

22

We are deprecating the Criteria API in lieu of JPA extension support.

Consider this:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
HibernateCriteria hc = cb.unwrap( HibernateCriteria.class );
...
query.where( hc.someAwesomeThing( ... ) );
List<SomeEntity> entities = entityManager.createQuery( query ).getResultList();

Contrary to comments, we do intend to continue to deliver Hibernate-specific features, but we want to introduce those through the standard API instead rather than trying to manage keeping two very different APIs that are meant to be complementary in sync.

Naros
  • 19,928
  • 3
  • 41
  • 71
  • 29
    Currently my project uses deprecated Criteria API. Is it recommended to rewrite queries with HQL or CriteriaBuilder according to new recommendations? – Justinas Jakavonis Jul 04 '17 at 07:39