不再使用Hibernate的旧版org.hibernate.Criteria API [英] Hibernate's legacy org.hibernate.Criteria API is deprecated

查看:1246
本文介绍了不再使用Hibernate的旧版org.hibernate.Criteria API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Hibernate 5时,它显示错误为:

As I am using Hibernate 5 and it shows error as:

2018年4月28日下午12:24:45 org.hibernate.internal.SessionImpl createCriteria警告:HHH90000022:Hibernate的遗产 不推荐使用org.hibernate.Criteria API;使用JPA 改为使用javax.persistence.criteria.CriteriaQuery

Apr 28, 2018 12:24:45 PM org.hibernate.internal.SessionImpl createCriteria WARN: HHH90000022: Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead

@Transactional
public class AccountDAOImpl implements AccountDAO {
    
    @Autowired
    private SessionFactory sessionFactory;


    @Override
    public ACCOUNTS findAccount(String userName) {
        // TODO Auto-generated method stub//
        
        Session session=sessionFactory.getCurrentSession();
        Criteria crit=session.createCriteria(ACCOUNTS.class).add(Restrictions.eq("user_name", userName));
        //crit.add(Restrictions.eq("user_name", userName));
        return (ACCOUNTS)crit.uniqueResult();
    }

}

界面帐户DAO

public interface AccountDAO {
    
     public ACCOUNTS findAccount(String userName );

}

和模型课程帐户

package org.vikas.shoppingCart.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="ACCOUNTS")
public class ACCOUNTS implements Serializable
{
    private static final long serialVersionUID = 1L;
    public static final String ROLE_MANAGER = "MANAGER";
    public static final String ROLE_EMPLOYEE = "EMPLOYEE";
    
    private String user_name;
    private String user_password;
    private boolean active;
    private String user_role;
    
    @Id
    @Column(name="user_name",length=50,nullable=false)
    public String getUser_name() {
        return user_name;
    }
    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
    
    @Column(name="user_password",length=20,nullable=false)
    public String getUser_password() {
        return user_password;
    }
    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }
    
    @Column(name="active",length=1,nullable=false)
    public boolean isActive() {
        return active;
    }
    public void setActive(boolean active) {
        this.active = active;
    }
    
    @Column(name="user_role",length=20,nullable=false)
    public String getUser_role() {
        return user_role;
    }
    public void setUser_role(String user_role) {
        this.user_role = user_role;
    }
    @Override
    public String toString() {
        return "ACCOUNTS [user_name=" + user_name + ", user_password=" + user_password + ", active=" + active
                + ", user_role=" + user_role + "]";
    }
}

正如我搜索到的那样,它说使用CreateBuilder来避免过时(因为我得到的示例仅显示List中的所有数据),但是我在这里使用了添加条件,但有限制.

As I searched and it says to use CreateBuilder to avoid deprecation(as i got example of only to show all data in List), but I am using add condition here with restrictions.

请帮助我如何在使用条件的同时使用createBuilder,就像我在代码中使用add(Restrictions.eq("user_name", userName));一样,还是有其他解决方案来避免弃用?

Kindly help me to how to use createBuilder while using condition like I used add(Restrictions.eq("user_name", userName)); in code or is there any other solution to avoid deprecation?

推荐答案

此日志是警告,不是错误:

This log is a warning not an error :

2018年4月28日下午12:24:45 org.hibernate.internal.SessionImpl createCriteria警告:HHH90000022:Hibernate的遗产 不推荐使用org.hibernate.Criteria API;使用JPA 改为使用javax.persistence.criteria.CriteriaQuery

Apr 28, 2018 12:24:45 PM org.hibernate.internal.SessionImpl createCriteria WARN: HHH90000022: Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead

它鼓励您在使用Hibernate时通过JPA API.

It encourages you to pass by the JPA API as you use Hibernate.

问题的根源在于,您操作Session(休眠)而不是EntityManager(JPA).
进行反向操作会引起实际代码中的许多更改,因为大多数导入操作以及处理方式都将有所不同.
你不能只改变一件事.

At the root of your problem, you manipulate a Session (Hibernate) instead of a EntityManager(JPA).
Doing the reverse will provoke many changes in your actual code as most of the imports and also the way to process will be different.
You cannot change just one thing.

这是JPA方式(包括进口商品):

Here is the JPA way (imports included) :

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

@Transactional
public class AccountDAOImpl implements AccountDAO {

    @Autowired
    private EntityManager em;    

    @Override
    public ACCOUNTS findAccount(String userName) {    
        final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<ACCOUNTS> crit = criteriaBuilder.createQuery(ACCOUNTS.class);
        Root<ACCOUNTS> root = crit.from(ACCOUNTS.class);
        crit.where(criteriaBuilder.equal(root.get("user_name"), userName))
            .distinct(true);
        return em.createQuery(crit).getSingleResult();      
    }

}

这篇关于不再使用Hibernate的旧版org.hibernate.Criteria API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆