-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Querying a collection
PostPosted: Thu Oct 07, 2010 10:05 am 
Newbie

Joined: Fri Jun 01, 2007 4:52 pm
Posts: 4
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-12m

When 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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.