Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecated createCriteria method in Hibernate 5

Tags:

java

hibernate

This calling is deprecated:

session.createCriteria(Bus.class).list(); 

In source files I can see this:

/** @deprecated */ @Deprecated Criteria createCriteria(Class var1);  /** @deprecated */ @Deprecated Criteria createCriteria(Class var1, String var2);  /** @deprecated */ @Deprecated Criteria createCriteria(String var1);  /** @deprecated */ @Deprecated Criteria createCriteria(String var1, String var2); 

But I can't understand which method I have to use instead of createCriteria.

like image 550
faoxis Avatar asked Nov 21 '16 12:11

faoxis


People also ask

Is hibernate criteria deprecated?

Since Hibernate 5.2, the Hibernate Criteria API is deprecated, and new development is focused on the JPA Criteria API. We'll explore how to use Hibernate and JPA to build Criteria Queries.

Is criteria query deprecated?

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.

What happened to the hibernate criteria API?

Since Hibernate 5.2, the Hibernate Criteria API is deprecated, and new development is focused on the JPA Criteria API. We'll explore how to use Hibernate and JPA to build Criteria Queries.

How to filter Hibernate session by criteria in Java?

The method Session.createCriteria() of Hibernate Session returns Criteria instance. We can filter the result using Restrictions class. The method Session.contains() checks if the given instance is available in Hibernate Session or not. The method Session.cancelQuery() cancels the execution of query.

How to add restriction using restrictions class of hibernate while fetching data?

Session.createCriteria () accepts entity name and returns the instance of org.hibernate.Criteria to which we can add restriction using Restrictions class of hibernate while fetching data. We use createCriteria () as follows. Find the example. Find the output. Session.contains () checks if the session contains the given instance.

What is transient and detached state in hibernate?

In hibernate every instance has three states transient, detached and persistent. If the instance is in transient or detached states, Session.contains () will return false and in case of persistent state, it returns true.


1 Answers

You can use the following interfaces instead in Hibernate 5.2 +:

javax.persistence.criteria.CriteriaBuilder javax.persistence.criteria.CriteriaQuery  // Create CriteriaBuilder CriteriaBuilder builder = session.getCriteriaBuilder();  // Create CriteriaQuery CriteriaQuery<YourClass> criteria = builder.createQuery(YourClass.class); 
like image 143
DimaSan Avatar answered Sep 18 '22 16:09

DimaSan