81

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.

faoxis
  • 1,912
  • 5
  • 16
  • 31

4 Answers4

98

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);
DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • Thank you! Can you recommend me what to do with `hibernate criteria` here: `((Role) criteria.add(Restrictions.eq("name", name)).uniqueResult()).getId();` ? – faoxis Nov 22 '16 at 09:44
  • 5
    You can use `CriteriaQuery where` method. More info here: https://docs.oracle.com/cd/E19798-01/821-1841/gjivi/index.html – DimaSan Nov 22 '16 at 10:25
  • 8
    For real code and a complete example: [Giflib hibernate: Updated to Hibernate 5.2 using the JPA Criteria API](https://github.com/treehouse/giflib-hibernate/commit/f97a2828a466e849d8ae84884b5dce60a66cf412) – Roeland Van Heddegem Sep 22 '17 at 11:28
  • I am changing my existing hibernate session into JPA entity manager instances and im changing all createCriteria query using suggested answer above, but i came across one statment like c.setCacheable(true) and c.setCacheRegion("generalCaache"), where c is Criteria instance(previously) which i have changed now to CriteriaQuery. What is the equivalent in the CriteriaQuery way ? – rinilnath Aug 14 '20 at 18:06
23

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(Restrictions.like("name", "string1", MatchMode.END)))
            .add(Restrictions.not(Restrictions.like("name", "string2", MatchMode.END)))
            .add(Restrictions.in("joinObject.id", ids));

    return criteria.list();
}

Changing that to use:

javax.persistence.criteria.CriteriaBuilder
javax.persistence.criteria.CriteriaQuery

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")
    );

    criteria.select(myObjectRoot).where(joinObject.get("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.

justMe
  • 2,200
  • 16
  • 20
  • I am changing my existing hibernate session into JPA entity manager instances and im changing all createCriteria query using suggested answer above, but i came across one statment like c.setCacheable(true) and c.setCacheRegion("generalCaache"), where c is Criteria instance(previously) which i have changed now to CriteriaQuery. What is the equivalent in the CriteriaQuery way ? – rinilnath Aug 14 '20 at 18:06
17

Adding Answer as of March 2018.

Dependencies:

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);
        criteriaQuery.from(Contact.class);

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

        return contacts;
    }

while the sessionFactory is:

public static SessionFactory buildSessionFactory() {
        final ServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
        return new MetadataSources(registry).buildMetadata().buildSessionFactory();
    }
STEEL
  • 8,955
  • 9
  • 67
  • 89
0

Try this :

cr.createCriteria("obj1", "obj1", JoinType.LEFT_OUTER_JOIN);
Amine ABBAOUI
  • 175
  • 1
  • 12