CHAPTER 15 Advanced query options session.createCriteria(User.class) .add( Restrictions.like(”username”,

CHAPTER 15 Advanced query options session.createCriteria(User.class) .add( Restrictions.like(”username”, “G%”) ); session.createCriteria(User.class) .add( Restrictions.like(”username”, “G”, MatchMode.START) ); The allowed MatchModes are START, END, ANYWHERE, and EXACT. You often also want to perform case-insensitive string matching. Where you d resort to a function such as LOWER() in HQL or JPA QL, you can rely on a method of the Criteria API: session.createCriteria(User.class) .add( Restrictions.eq(”username”, “foo”).ignoreCase() ); You can combine expressions with logical operators. Combining expressions with logical operators If you add multiple Criterion instances to the one Criteria instance, they re applied conjunctively (using and): session.createCriteria(User.class) .add( Restrictions.like(”firstname”, “G%”) ) .add( Restrictions.like(”lastname”, “K%”) ); If you need disjunction (or), there are two options. The first is to use Restrictions.or() together with Restrictions.and(): session.createCriteria(User.class) .add( Restrictions.or( Restrictions.and( Restrictions.like(”firstname”, “G%”), Restrictions.like(”lastname”, “K%”) ), Restrictions.in(”email”, emails) ) ); The second option is to use Restrictions.disjunction() together with Restrictions.conjunction(): session.createCriteria(User.class) .add( Restrictions.disjunction() .add( Restrictions.conjunction() .add( Restrictions.like(”firstname”, “G%”) ) .add( Restrictions.like(”lastname”, “K%”) ) ) .add( Restrictions.in(”email”, emails) ) );
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Leave a Reply