-->
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: HQL: from class where field in ( :set ), what is possible?
PostPosted: Sat Mar 28, 2009 6:13 pm 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
I've seen the example in 10.4.1.4
Code:
List names = new ArrayList();
names.add("Izi");
names.add("Fritz");
Query q = sess.createQuery("from DomesticCat cat where cat.name in (:namesList)");
q.setParameterList("namesList", names);
List cats = q.list();


Which indicates that a named argument to a set can be an ArrayList of strings.
But does not inform as to what is fully possible.
[and maybe things have changed since that doc?
even an ArrayList full of string does not work for me, even when selecting on a string-valued field]

Three questions:
1) Can the binding argument be other than a List? can it be a java.util.Set, or a simple array?
Do generic types matter (i've tried both ways, no joy)

2) Is this notation expected to work for non-String valued fields? (like int or Integer)

3) When I bind a simple value for :arg in a query like: from Activity as act where act.id in (:arg)
it works (but only selects for that single value);
but binding :arg to any collection or array gets a ClassCastException;
As below, where i pass an ArrayList for an Integer-valued field. (does int vs Integer matter?)
Can you suggest why? or how to workaround?

Quote:
java.lang.ClassCastException: java.util.ArrayList
at org.hibernate.type.IntegerType.set(IntegerType.java:41)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:38)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)


Answers and insight will be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 28, 2009 6:50 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Can the binding argument be other than a List? can it be a java.util.Set, or a simple array?


If you check the various Query.setParameterList() methods you can see that you can use any Collection (includes Set, List, etc) and Object[]. Thus, it will not work with primitive arrays (int[], etc.).

Quote:
Is this notation expected to work for non-String valued fields?


As far as I know it works for all types.

Quote:
but binding :arg to any collection or array gets a ClassCastException;


Are you sure that you are using one the Query.setParameterList() methods and not Query.setParameter(), Query.setString() or another method that works with single values only?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 07, 2009 2:05 am 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
I'm sure you are right; I didn't notice the 'List' suffix on .setParameterList()
and I was recycling a test routine that uses .setParameter()

Thanks for pointing out the obvious :)
[sometimes that the hardest part to see]


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.