Using Hibernate criteria

Posted on 07-01-2013 13:06 by graham
In this tutorial we will show use how to build Hibernate queries using Hibernate Criteria.

Creating criteria for a model class


Let's say we have a persistent class Person. Assuming we are in a context where the Hibernate session is available, we can create a simple criteria object for this class in the following manner:
Criteria crit = hibernateSession.createCriteria(Person.class);


Query by property


Having the Criteria object, we can add a simple condition on one of its properties and retrieve results for such query.Let's say we want to find all persons whose name is "John".
Criteria crit = hibernateSession.createCriteria(Person.class);
crit.add(Restrictions.eq("firstName", "John"));

// execute query
List<Person> foundRecords = (List<Person>)crit.list();


Condition alternative


If we want to find people whole name is "John" or who are taller than 180cm (just a silly example, why would we want that?), we can use the logical alternative operation Restrictions.or:
Criteria crit = hibernateSession.createCriteria(Person.class);
Criterion nameCondition = Restrictions.eq("firstName", "John");
Criterion heightCondition = Restrictions.ge("height", 180);
crit.add(Restrictions.or(nameCondition, heightCondition));

// execute query
List<Person> foundRecords = (List<Person>)crit.list();


Conditions on a nested property


Now let's say our class Person has property company denoting a company this person works form. Property company is of type Company - also a persistent class.

Let's say we want to put some condition on the company property, e.g. find people working only for a company whose name contains the phrase "Wordgraphs". In order to do this, we need to create an alias for the property and then define the condition for this alias:
Criteria crit = hibernateSession.createCriteria(Person.class);

// create subcriteria
Criteria companyCrit = crit.createCriteria("company", "company");
companyCrit.add(Restrictions.ilike("company.name", "%Wordgraphs%"));

// execute query
List<Person> foundRecords = (List<Person>)crit.list();

A short explanation might be necessary for the following construction:
Restrictions.ilike("company.name", "%Wordgraphs%")

Restrictions.ilike performs a case-insensitive comparison, and the percent marks in "%Wordgraphs%" are wildcards that denote any sequence of characters, possibly also empty.
Comments

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems