Criteria instance - II

31 August 2008

In 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.

del.icio.us:Criteria instance - II  digg:Criteria instance - II  spurl:Criteria instance - II  wists:Criteria instance - II  simpy:Criteria instance - II  newsvine:Criteria instance - II  blinklist:Criteria instance - II  furl:Criteria instance - II  reddit:Criteria instance - II  fark:Criteria instance - II  blogmarks:Criteria instance - II  Y!:Criteria instance - II  smarking:Criteria instance - II  magnolia:Criteria instance - II  segnalo:Criteria instance - II  gifttagging:Criteria instance - II

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

Leave a Reply