Java Reference
In-Depth Information
public static SimpleExpression like(String propertyName,
String value,
MatchMode matchMode)
The first like() or ilike() method takes a pattern for matching. Use the % character as
a wildcard to match parts of the string, like so:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.like("name","Mou%"));
List results = crit.list();
The second like() or ilike() method uses an org.hibernate.criterion.MatchMode
object to specify how to match the specified value to the stored data. The MatchMode object
(a type-safe enumeration) has four different matches:
ANYWHERE : Anyplace in the string
END : The end of the string
EXACT : An exact match
START : The beginning of the string
Here is an example that uses the ilike() method to search for case-insensitive matches
at the end of the string:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.ilike("name","browser", MatchMode.END));
List results = crit.list();
The isNull() and isNotNull() restrictions allow you to do a search for objects that have
(or do not have) null property values. This is easy to demonstrate:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.isNull("name"));
List results = crit.list();
Several of the restrictions are useful for doing math comparisons. The greater-than
comparison is gt() , the greater-than-or-equal-to comparison is ge() , the less-than com-
parison is lt() , and the less-than-or-equal-to comparison is le() . We can do a quick
retrieval of all products with prices over $25 like this:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.gt("price",new Double(25.0)));
List results = crit.list();
Moving on, we can start to do more complicated queries with the Criteria API. For exam-
ple, we can combine AND and OR restrictions in logical expressions. When you add more than
one constraint to a criteria query, it is interpreted as an AND , like so:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.gt("price",new Double(25.0)));
crit.add(Restrictions.like("name","K%"));
List results = crit.list();
Search WWH ::




Custom Search