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
.
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.
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.
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With