当我使用Hibernate 5时,它显示错误:
2018年4月28日下午12:24:45 org.hibernate.internal.SessionImpl createCriteria警告:HHH90000022:Hibernate的遗产 org.hibernate.Criteria API已弃用;使用JPA 而是javax.persistence.criteria.CriteriaQuery
@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();
}
}
interaface 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)); 这样的条件,还是有其他解决方案可以避免弃用?
答案 0 :(得分:1)
此日志是警告而非错误:
2018年4月28日下午12:24:45 org.hibernate.internal.SessionImpl createCriteria警告:HHH90000022:Hibernate的遗产 org.hibernate.Criteria API已弃用;使用JPA 而是javax.persistence.criteria.CriteriaQuery
它鼓励您在使用Hibernate时通过JPA API。
问题的根源是,您操纵Session
(休眠)而不是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();
}
}
答案 1 :(得分:0)
您可以使用JPA的CriteriaBuilder
API,如下所示
// Create entity manager factory first
// Make sure the package name you specify `com.db.entity` matched what you specify in `pesistence.xml`
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("com.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(Accounts.class);
cq.where(cb.equal(join.get("name"), "ordinary"));
TypedQuery<Accounts> tq = em.createQuery(cq);
List<Accounts> accounts = tq.getResultList();
return accounts;