在 Hibernate 5中不推荐的 createCriteria 方法

这种说法不受欢迎:

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

在源文件中我可以看到:

/** @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);

但是我不能理解我必须用哪种方法来代替 createCriteria

75207 次浏览

您可以在 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);

添加截至2018年3月的答案。

依赖性:

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;
}

sessionFactory是:

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

出于安全原因,我使用了以下方法,更改了对象名称:

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();
}

改为使用:

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

查询如下所示:

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();
}

希望对其他人有所帮助,请随时提出任何改进代码的建议。

试试这个:

cr.createCriteria("obj1", "obj1", JoinType.LEFT_OUTER_JOIN);