What is Criteria API in Hibernate?

Answered

What is Criteria API in Hibernate?

Ninja Asked on 17th September 2018 in Hibernate.
Add Comment
1 Answer(s)
Best answer

Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like “search” where there is a variable number of conditions to be placed upon the result set. Complex Hibernate queries can be generated on-the-fly using Criteria API.

A Criteria object is created using the createCriteria() method in the Hibernate session object:

Criteria criteria = session.createCriteria(Student.class);
criteria.setMaxResults(10);
List students = criteria.list();

Once created, Criterion instances are usually obtained via the factory methods on Restrictions.

List students = session.createCriteria(Student.class).add(Restrictions.like("studentName", "James%")).list();
Ninja Answered on 17th September 2018.
Add Comment