java – Deprecated createCriteria method in Hibernate 5-ThrowExceptions

Exception or error:

This calling is deprecated:


In source files I can see this:

/** @deprecated */
Criteria createCriteria(Class var1);

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

/** @deprecated */
Criteria createCriteria(String var1);

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

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

How to solve:

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


// Create CriteriaBuilder
CriteriaBuilder builder = session.getCriteriaBuilder();

// Create CriteriaQuery
CriteriaQuery<YourClass> criteria = builder.createQuery(YourClass.class);


I had the following method, changed the object names for security reasons:

public List<MyObject> listAllForIds(List<Long> ids) {
    Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(MyObject.class)
            .createAlias("joinObject", "joinObject")
            .add(Restrictions.not("name", "string1", MatchMode.END)))
            .add(Restrictions.not("name", "string2", MatchMode.END)))
            .add("", ids));

    return criteria.list();

Changing that to use:


The query look like the following:

public List<MyObject> listAllForIds(List<Long> ids) {

    CriteriaBuilder builder = getSessionFactory().getCurrentSession().getCriteriaBuilder();
    CriteriaQuery<MyObject> criteria = builder.createQuery(MyObject.class);
    Root<MyObject> myObjectRoot = criteria.from(MyObject.class);
    Join<MyObject, JoinObject> joinObject = myObjectRoot.join("joinObject");

    Predicate likeRestriction = builder.and(
            builder.notLike( myObjectRoot.get("name"), "%string1"),
            builder.notLike( myObjectRoot.get("name"), "%string2")
    );"id").in(ids), likeRestriction);

    TypedQuery<MyObject> query = getSessionFactory().getCurrentSession().createQuery(criteria);

    return query.getResultList();

Hope it helps someone else, please feel free to suggest any chanages to improve the code.


Adding Answer as of March 2018.


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;

import javax.persistence.criteria.CriteriaQuery;
import java.util.List;

public static List<Contact> fecthAllContacts() {
        Session session = sessionFactory.openSession();

        // create Criteria
        CriteriaQuery<Contact> criteriaQuery = session.getCriteriaBuilder().createQuery(Contact.class);

        List<Contact> contacts = session.createQuery(criteriaQuery).getResultList();

        return contacts;

while the sessionFactory is:

public static SessionFactory buildSessionFactory() {
        final ServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
        return new MetadataSources(registry).buildMetadata().buildSessionFactory();


from the API documentation:

Criteria createCriteria(Class persistentClass)
Deprecated. (since 5.2) for Session, use the JPA Criteria
Create Criteria instance for the given class (entity or subclasses/implementors).
persistentClass – The class, which is an entity, or has entity subclasses/implementors
The criteria instance for manipulation and execution

So it briefly says to…

use the JPA Criteria Create Criteria

Leave a Reply

Your email address will not be published. Required fields are marked *