Java Hibernate 5 中弃用的 createCriteria 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40720799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:52:56  来源:igfitidea点击:

Deprecated createCriteria method in Hibernate 5

javahibernate

提问by faoxis

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.

但我无法理解我必须使用哪种方法而不是createCriteria.

采纳答案by DimaSan

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

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

回答by pleft

from the API documentation:

来自 API 文档:

@Deprecated 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). Parameters: persistentClass - The class, which is an entity, or has entity subclasses/implementors Returns: The criteria instance for manipulation and execution

@Deprecated Criteria createCriteria(Class persistentClass) 已弃用。(自 5.2 起)对于 Session,使用 JPA Criteria Create Criteria 实例为给定类(实体或子类/实现器)。参数:persistentClass - 类,它是一个实体,或具有实体子类/实现器返回:操作和执行的标准实例

So it briefly says to...

所以它简要地说...

use the JPA Criteria Create Criteria

使用 JPA 标准创建标准

回答by STEEL

Adding Answer as of March 2018.

截至 2018 年 3 月添加答案。

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 sessionFactoryis:

sessionFactory为:

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

回答by justMe

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.

希望它可以帮助其他人,请随时提出任何改进代码的建议。