Reputation: 93
As I am using Hibernate 5 and it shows error as:
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();
}
}
interaface accountDAO
public interface AccountDAO {
public ACCOUNTS findAccount(String userName );
}
and model class accounts
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 + "]";
}
}
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.
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?
Upvotes: 3
Views: 32647
Reputation: 1
An very easy way to migrate hibernate legacy criteria is to use BaseCriteria: https://awsdc.gitlab.io/
Upvotes: 0
Reputation: 131396
This log is a warning not an error :
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
It encourages you to pass by the JPA API as you use Hibernate.
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.
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();
}
}
Upvotes: 3
Reputation: 2814
You can use JPA's CriteriaBuilder
API like below
// 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;
Upvotes: 1