gpt4 book ai didi

java - Hibernate 的旧版 org.hibernate.Criteria API 已弃用

转载 作者:行者123 更新时间:2023-12-02 11:18:54 25 4
gpt4 key购买 nike

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

Apr 28, 2018 12:24:45 PM org.hibernate.internal.SessionImplcreateCriteria WARN: HHH90000022: Hibernate's legacyorg.hibernate.Criteria API is deprecated; use the JPAjavax.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();
}

}

接口(interface)accountDAO

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 中的所有数据),但我正在使用添加条件这里有限制。

请帮助我如何在使用条件时使用 createBuilder ,就像我在代码中使用 add(Restrictions.eq("user_name", userName)); 或者是否存在还有其他解决方案可以避免弃用吗?

最佳答案

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

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。

问题的根源在于,您操作的是 Session (Hibernate),而不是 EntityManager(JPA)。
执行相反的操作会导致实际代码发生许多变化,因为大多数导入以及处理方式都会有所不同。
你不能只改变一件事。

这是 JPA 方式(包括导入):

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

}

关于java - Hibernate 的旧版 org.hibernate.Criteria API 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50073875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com