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.  [ 3 posts ] 
Author Message
 Post subject: using query.setParameter(position, value) with in and a List
PostPosted: Mon Nov 06, 2006 4:38 pm 
Newbie

Joined: Sat Mar 11, 2006 4:15 pm
Posts: 15
So I need to set a JDBC style parameter to a List with the in "in" operator. I can't used named parameters in this instance so it has to be set by position. I'm trying to do a query like the following:

from Document d d.id in (?)

and I pass in a list of Integers as the parameter. But I get a ClassCastException: ArrayList with the following stack trace:

# org.hibernate.type.IntegerType.set(IntegerType.java:41)
# org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
# org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
# org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1514)
# org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1576)
# org.hibernate.loader.Loader.doQuery(Loader.java:661)
# org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
# org.hibernate.loader.Loader.doList(Loader.java:2145)
# org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
# org.hibernate.loader.Loader.list(Loader.java:2024)
# org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:392)
# org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:333)
# org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
# org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
# org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)

How can you use the parameters by position in a query with a List?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 5:00 pm 
Newbie

Joined: Mon Nov 06, 2006 3:00 pm
Posts: 2
Location: Maryland
use a for loop to add the
Code:
StringBuffer sb = new StringBuffer();
sb.append("select a, b, c\n");
sb.append("from Document d d.id in (");
for (int i=0; i < subList.size(); i++)
{
  if (i != 0) {
    sb.append(",");
  }
  sb.append("?");
}
sb.append(")");

Query query = session.createQuery(sb.toString());
for (int i=0; i< subList.size(); i++) {
  query.setParameter(i, subList[i]);
}



Hope it works for you or give your some ideas.

_________________
~ShoKu~


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 5:07 pm 
Newbie

Joined: Sat Mar 11, 2006 4:15 pm
Posts: 15
The problem is that I'm doing this in a method that takes an arbitrary query with an arbitrary number of parameters so doing it that way could get pretty hairy.


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

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.