I am wondering what the best practice for this situation would be, here is code that works, notice the if(itemKey == null) condition, what I would like to have there in the else portion is:
q += " and acl.itemKey = ?;
But I can't do that unless I change the whole sess.find statement and doing some more conditionals, is there a cleaner way?
Code:
String q = "select acl from " + AccessLevel.class.getName()
+ " acl where acl.groupKey = ? "
+ " and acl.realm = ?";
if (itemKey == null) {
q += " and acl.itemKey IS NULL";
} else {
q += " and acl.itemKey = '" + itemKey + "'";
}
//Couldn't do the IS NULL condition above using prepared statement, so took it out
// todo: sql escape the itemKey string
List groups = sess.find(q,
new Object[]{group.getId(), realm},
new Type[]{Hibernate.INTEGER, Hibernate.STRING});
[/code]