-->
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.  [ 4 posts ] 
Author Message
 Post subject: IN keyword with list of Strings
PostPosted: Fri Aug 17, 2007 6:48 am 
Beginner
Beginner

Joined: Thu May 17, 2007 8:37 am
Posts: 22
Location: London
I'm sure this is an easy one.

I've got a list of strings that I want to use as a parameter for a query:
Code:
String[] list = {'A', 'B', 'C'};

entityManager.createQuery("select a from ObjectA as a where a.field IN :list").setParameter("list", list).getResultList();

If I try to use the query as above it throws a org.hibernate.hql.ast.QuerySyntaxException. It doesn't like a parameter being used with the "IN" keyword.

My (nasty) workaround is:
Code:
String list = "('A', 'B', 'C')";

entityManager.createQuery("select a from ObjectA as a where a.field IN "+list).getResultList();


Have I missed something or can you not pass a parameter to an IN?

Thanks,

Damian.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 6:51 am 
Newbie

Joined: Mon Jul 31, 2006 2:15 am
Posts: 4
try

Code:
String[] list = {'A', 'B', 'C'};

entityManager.createQuery("select a from ObjectA as a where a.field IN (:list)").setParameterList("list", list).getResultList();


yours
klaus


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 7:16 am 
Beginner
Beginner

Joined: Thu May 17, 2007 8:37 am
Posts: 22
Location: London
Thanks.

I'm using Seam which uses the javax.persistence.EntityManager. CreateQuery returns a javax.persistence.Query which doesn't have the setParameterList method.

So if I wanted to use this I could do:
Code:
String[] list = {'A', 'B', 'C'};
Session session = (Session)entityManager.getDelegate();
session.createQuery("select a from ObjectA as a where a.field IN (:list)").setParameterList("list", list).getResultList();


Cheers,

Damian.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 7:34 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Try using a List instead of an array.
i.e.
Code:
String[] array = {'A', 'B', 'C'};
List list = Arrays.asList(array);
Session session = (Session)entityManager.getDelegate();
session.createQuery("select a from ObjectA as a where a.field IN (:list)").setParameterList("list", list).getResultList();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.