Back in July I blogged about how to do AND/OR type SQL queries using Hibernate AND/OR JPA using disjunctions. If you looked at the example code you will have seen that I was appending "%" as the wildcard operator in my Restrictions.
Since then I've used Restrictions a couple more times and wondered if there was a better way of specifying them other than string concatenation.
Well there is and it's with the use of the MatchMode class.
Rather than this code:
Since then I've used Restrictions a couple more times and wondered if there was a better way of specifying them other than string concatenation.
Well there is and it's with the use of the MatchMode class.
Rather than this code:
import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Restrictions; Criteria criteria = session.createCriteria(Product.class); criteria.add( Restrictions.disjunction() .add(Restrictions.ilike("code", codeOrName + "%")) .add(Restrictions.ilike("name", "%" + codeOrName + "%")) ); return criteria.list();You can now write:
import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Restrictions; Criteria criteria = session.createCriteria(Product.class); criteria.add( Restrictions.disjunction() .add(Restrictions.ilike("code", codeOrName, MatchMode.START)) .add(Restrictions.ilike("name", codeOrName, MatchMode.ANYWHERE)) ); return criteria.list();
Comments
(f.cking captcha!)