Reputation: 127
From Hibernate 5 Create criteria has been deprecated. Instead we can use Create query. But how to add restrictions now. Please anyone help me with this
public List<ExpensesSummary> displayExpense() {
Session session = getSessionFactory().openSession();
CriteriaBuilder builer = session.getCriteriaBuilder();
CriteriaQuery<ExpensesSummary> criteria = builer.createQuery(ExpensesSummary.class);
List<ExpensesSummary> expensesList = session.createQuery(criteria).getResultList();
return expensesList;
}
Upvotes: 2
Views: 6334
Reputation: 126
Example for CriteriaBuilder with restrictions of CriteriaBuilder methods. The JPQL for this Criteria.
select i from Item i where i.name="SomeThing"
Now with CriteriaBuilder...
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(
builder.equal(i.get("name"), "SomeThing")
);
TypedQuery<Role> query = session.createQuery(criteria);
Now you can get the result from the query by getSingleResult() or getResultList(). and if you want to know about more restriction methods of CriteriaBuilder Click Here.
Upvotes: 0
Reputation: 943
Below example, may help you.
final CriteriaBuilder builder = getSessionFactory().getCurrentSession().getCriteriaBuilder();
CriteriaQuery<Customer> criteriaQuery = builder.createQuery(Customer.class);
Root<Customer> Customer = criteriaQuery.from(Customer.class);
criteriaQuery.where(builder.equal(builder.upper(customer.get("customerName")),customerName.toUpperCase()));
Query<Customer> query = getSessionFactory().getCurrentSession().createQuery(criteriaQuery);
final List<Customer> results = query.getResultList();
Upvotes: 0
Reputation: 2650
Session.getCriteriaBuilder()
method is inherited from javax.persistence.EntityManager
interface. So criteria building is done accoriding to JPA standard.
You can check on this resource for JPA reference: WHERE in Criteria Queries
Upvotes: 1