I've done optional parameters in a single SQL query before. I just used a part of the WHERE clause like this:
Code:
AND ( :parameter IS NULL OR c.column = :parameter )
The only problem was that Hibernate needed the type specified when null was passed. So I had to do this:
Code:
query.setParameter("parameter", parameter, Hibernate.LONG);
instead of the usual:
Code:
query.setParameter("parameter", parameter);
In my case I don't need to query for null, so I use it to indicate not to check for the value. If you do need to query for null as well as other values then you could probably use two parameters. The first to indicate that the second should be checked for.