Criteria instance - II
31 August 2008In this post, I will show how Restrictions may be grouped logically.
List students = sess.createCriteria(Student.class) .add( Restrictions.like("name", "Laiq%") ) .add( Restrictions.or( Restrictions.eq( "age", new Integer(0) ), Restrictions.isNull("age") ) ) .list();
List Students = sess.createCriteria(Student.class) .add( Restrictions.in( "name", new String[] { "Laiq", "Izi", "Pk" } ) ) .add( Restrictions.disjunction() .add( Restrictions.isNull("age") ) .add( Restrictions.eq("age", new Integer(20) ) ) .add( Restrictions.eq("age", new Integer(21) ) ) .add( Restrictions.eq("age", new Integer(22) ) ) ) ) .list();
There are many built-in criterion types (Restrictions subclasses), but one that is especially useful lets you specify SQL directly.
List students = sess.createCriteria(Student.class) .add( Restrictions.sqlRestriction("lower({alias}.name) like lower(?)", "Laiq%", Hibernate.STRING) .list();
Hope this helps.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Criteria instance - II