I have an entity that has a set of arbitrary categories attached to it, it's a one-to-many unidirectional relationship using a join table. Basically, a textbook example as illustrated @
http://docs.jboss.org/hibernate/core/3. ... l-join-12mWhen I query the entity based on the collection of categories I have to use the following;
Code:
public List getByEntityFilter(EntityFilter filter) {
Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Entity.class);
if (filter.getCategories() != null) {
addCategories(criteria, filter.getCategories());
}
return criteria.list();
}
private void addCategories(Criteria criteria, Set<EntityCategory> categories) {
if (categories.size() > 0) {
Long[] ids = new Long[categories.size()];
int i = 0;
for (EntityCategory category : categories) {
ids[i] = category.getCategoryId();
i++;
}
criteria.createAlias("categories", "cat")
.add(Restrictions.in("cat.categoryId", ids));
}
}
I tried using the following;
Code:
criteria.add(Restrictions.in("categories", filter.getCategories()))
The second, much less verbose technique will not work for me and throws a
java.sql.SQLException: No value specified for parameter 1 exception. Is there a more simple way for me to accomplish my query using the Criteria API? I feel like I'm missing something here as I can't imagine querying via collection is that uncommon.