How to do AND/OR type SQL queries using Hibernate AND/OR JPA using disjunctions

If you have been using Hibernate or JPA for a while you will know that the Criteria and Restrictions classes are very handy for querying the database without writing any SQL code and without worrying about the real column names (these are all captured in the annotations that you have applied to the domain objects).

This is fine when you want to search for objects (rows) which are a combination of columns such as forename = "Andrew" AND surname = "Beacock" but what if you want to search for all people who either have a forename of Andrew and/or a surname of Beacock?

This is where a "disjunction" comes in. This is one of the types hanging off the Restrictions class and deals with AND/OR situations. The code below is looking through the 'products' table for any product that has a code or name starting with the text supplied:

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
Criteria criteria = session.createCriteria(Product.class);
    .add(Restrictions.ilike("code", codeOrName + "%"))
    .add(Restrictions.ilike("name", codeOrName + "%"))
return criteria.list();

You can obviously add as many additional Restrictions as you like to the disjunction to make it as simple or complex as you like...

iPhone version of the excellent Password Composer

I've been using the excellent Password Composer Greasemonkey script for Firefox for over 2 years now - it's a great way to ensure that your master password isn't spread around the web yet never gets in the way of accessing a site.

My collegue Andy Kayley recently pointed out that there is an iPhone version available so that you can easily access your Password Composer encrypted passwords whilst on the move!  It's written by Jon Parise and not only does he include a link to his version but also goes into excellent detail on how he developed the iPhone application!

Now I just need to get myself an iPhone... ;)