Hi guys,
I'm using the Generic DAO pattern from Hibernate in Action for my JBoss application, but I'm faced with the following problem.
When a parameter in a named query is a collection (e.g. "select b from MyBean b where b.id in (:collectionparam)") and the parameter is set with an empty collection, then an exception is thrown. This is normal, because the EJB specs say that the collection should have at least one entry.
This means that for each method of each implementation of a DAO where collections are used, an extra check must be performed.
For instance:
Code:
public List<MyBean> getByIds(Collection<Integer> ids) {
// exra check
if ( (ids == null) || (ids.size == 0) ) return new LinkedList<MyBean>();
// the rest of the code
Query query = getSession.getNamedQuery(MYQUERY);
query.setParameter("collectionparam", ids);
return query.list();
}
Does anyone have a proposal so that this simple check does not need to be replicated? (Perhaps by using an abstract class for each DAO, but wouldn't this be too much overhead....)
Any input would help