CHAPTER 10 n ADVANCED QUERIES USING CRITERIA 215 (Web hosting domains)

CHAPTER 10 n ADVANCED QUERIES USING CRITERIA 215 n ADVANCED QUERIES USING CRITERIA 215 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 comparison 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 example, we can combine ANDand 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();
We recommend high quality webhost to host and run your jsp application: christian web host services.

Leave a Reply