Hibernate Criteria API
Posted on: April 4, 2011 at 12:00 AM
In this tutorial you will learn about the API of the hibernate criteria

Hibernate Criteria API

The API (Application Programming Interface) of Hibernate Criteria provides an elegant way of building dynamic query on the persistence database.

The hibernate criteria API is very Simplified API for fetching data from Criterion objects. The criteria API is an alternative of HQL (Hibernate Query Language) queries. It is more powerful and flexible for writing tricky criteria functions and dynamic queries.

Using Criteria API to create criteria

Criteria criteria = session.createCriteria(Student.class);

The Criteria API supports all the rich sets of comparision operators such as =, <, >, ?, etc in the supported Expression class eq(), lt(), gt(), le() respectively.

for example -

Criteria criteria=session.createCriteria(Student.class);
	criteria.add(Restrictions.eq("name", "John"));
	criteria.add(Restrictions.le("date", date));

The Criteria API also provides some additional Comparison operators example- isNull, isNotNull, in, like, between.

Criteria criteria = session.createCriteria(Student.class);
	criteria.add(Restrictions.between("rollNo", 1, 5));
	criteria.add(Restrictions.like("name", "John"));
	List list = new ArrayList<Student>();
	criteria.add(Restrictions.in("rollNo", list));

API also provides operators like ltProperty, eqProperty etc. Example :-

Criteria criteria = session.createCriteria(Student.class);
	criteria.add(Restrictions.ltProperty("rollNo", "age"));

createAlias() method in Crteria API.

	.add(Restrictions.gt("roll.number", new Integer(40))).list();

Related Tags for Hibernate Criteria API:



Advertisement null