Welcome Guest  |  Register  |  Login
Login Name Password
  Search  
  Index  | Recent Threads  | Unanswered Threads  | Who's Online  | Help


Quick Go »

No member browsing this thread
Thread Status: Active
Total posts in this thread: 5
[ Jump to Last Post ]
Post new Thread
Author
Previous Thread This topic has been viewed 2365 times and has 4 replies Next Thread
Male o_swas
Member




Joined: Aug 8, 2007
Post Count: 55
Status: Offline
Reply to this Post  Reply with Quote 
Using an "in" Clause in JDOQL

Hello,

Consider this simple SQL statement that selects all Cars that are red, blue, or green:

select * from CAR where color in ('red', 'blue', 'green');


Now, assume that I want the equivalent query using JDOQL, plus the number of parameters in the "in" clause can be variable -- for example, one query may look for just 'red' while another might look for 'red', 'blue', and 'green'.

How would this query look in JDOQL, especially if I use it a NamedQuery, and how would I specify the parameters?

I'm guessing something like this:

select from com.mydomain.Car where color == :color


But how do I specify the mutiple parameter values?

Thank you!!

-Ryan
[Apr 22, 2008 8:08:04 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
Male ganzuoni
Expert
Member's Avatar


Joined: Nov 16, 2005
Post Count: 1008
Status: Online
Reply to this Post  Reply with Quote 
Re: Using an "in" Clause in JDOQL

Hello,

Consider this simple SQL statement that selects all Cars that are red, blue, or green:

select * from CAR where color in ('red', 'blue', 'green');


Now, assume that I want the equivalent query using JDOQL, plus the number of parameters in the "in" clause can be variable -- for example, one query may look for just 'red' while another might look for 'red', 'blue', and 'green'.

How would this query look in JDOQL, especially if I use it a NamedQuery, and how would I specify the parameters?

I'm guessing something like this:

select from com.mydomain.Car where color == :color


But how do I specify the mutiple parameter values?

Thank you!!

-Ryan

Declare color to be a java.util.Collection and call
query.execute(c)
where c is filled with the requested values

Guido
----------------------------------------
Guido Anzuoni
http://www.objectmagic.org
[Apr 23, 2008 7:57:44 AM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
Male o_swas
Member




Joined: Aug 8, 2007
Post Count: 55
Status: Offline
Reply to this Post  Reply with Quote 
Re: Using an "in" Clause in JDOQL

Thanks Guido, I was hoping it was something like that. Since I prefer to use named query parameters (like :color in the JDOQL) can I do something like this:

Collection colors = new ArrayList();
colors.add("red");
colors.add("blue");

Map params = new HashMap();
params.put("color", colors);

query.executeWithMap(params);

Is this correct?

Thank you!!
[Apr 23, 2008 1:59:12 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
Male ganzuoni
Expert
Member's Avatar


Joined: Nov 16, 2005
Post Count: 1008
Status: Online
Reply to this Post  Reply with Quote 
Re: Using an "in" Clause in JDOQL

Thanks Guido, I was hoping it was something like that. Since I prefer to use named query parameters (like :color in the JDOQL) can I do something like this:

Collection colors = new ArrayList();
colors.add("red");
colors.add("blue");

Map params = new HashMap();
params.put("color", colors);

query.executeWithMap(params);

Is this correct?

Thank you!!

Yes smile smile

Guido
----------------------------------------
Guido Anzuoni
http://www.objectmagic.org
[Apr 23, 2008 2:24:22 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
Male kberg
Newbie



USA
Joined: Jun 28, 2010
Post Count: 2
Status: Offline
Reply to this Post  Reply with Quote 
Re: Using an "in" Clause in JDOQL

I was never able to get the above syntax to work. I always received the error:

Parameter "color" was specified as java.util.ArrayList but should have been java.lang.String

However this syntax did work...

qry.setFilter(":color.contains(color)");

----------------------------------------
KC Berg
[Jun 28, 2010 3:15:09 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
[ Jump to Last Post ]
Show Printable Version of Thread  Post new Thread