Example & Tutorial understanding programming in easy ways.

What is Hibernate QBC (Query By Criteria)?

QBC (Query By Criteria): Hibernate provides the some different way to manipulating object and in turn data available in RDBMS tables. One of the methods is the Criteria API which allwos you to build up a criteria query object programmatically where you can apply filtration rules and logical condition.


In the Hibernate, Hibernate Session interface provides createCriteria() method which can be used to create a Criteria object that returns instances of the persistence object's class when your application executes a criteraia query.


We have an simple example of a criteria query is one which will simply return every object that corresponds to the Employee class.


Criteria cr = session.createCriteria(Employee.class);

List results = cr.list();


Restrictions with Criteria:


We can use add() method available for Criteria object to add restriction for a criteria query. Following is the example to add a restriction to return the records with salary is equal to 12000:


Criteria cr = session.createCriteria(Employee.class);

cr.add(Restrictions.eq("salary", 12000));

List results = cr.list();

Read More →