You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by duro <ju...@gmail.com> on 2010/05/20 17:00:59 UTC

java.lang.IllegalArgumentException: no-named-params

Hello, when i try to set parameter to a query like this:
Query query = em
				.createNativeQuery(
						"SELECT id FROM :table WHERE (next_run - now() < interval '2
minutes')",
						Long.class);
		query.setParameter("table", HARVEST_SCHEDULES_TABLE);

i get this error:

Exception in thread "Thread-1" java.lang.IllegalArgumentException:
no-named-params
	at
org.apache.openjpa.persistence.QueryImpl.setParameter(QueryImpl.java:1066)
	at org.apache.openjpa.persistence.QueryImpl.setParameter(QueryImpl.java:79)

what am i doing wrong? thanks a lot

ps: using openjpa 2.0.0
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/java-lang-IllegalArgumentException-no-named-params-tp5080072p5080072.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: java.lang.IllegalArgumentException: no-named-params

Posted by Michael Dick <mi...@gmail.com>.
I agree with Pinaki, but if you absolutely must use printf you can use %%.

-mike

On Tue, May 25, 2010 at 10:21 AM, Pinaki Poddar <pp...@apache.org> wrote:

>
> Binding query parameters have many advantages over using literals in the
> query string by whatever fancy string concatenation or printf like
> formatting. In fact, hard coding query parameters in the query string is a
> poor (and can even be risky) practice.
>
> Agreed that table name itself as a parameter is not liked by some databases
> and one can make a concession on that.
>
> -----
> Pinaki
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/java-lang-IllegalArgumentException-no-named-params-tp5080072p5098951.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: java.lang.IllegalArgumentException: no-named-params

Posted by Pinaki Poddar <pp...@apache.org>.
Binding query parameters have many advantages over using literals in the
query string by whatever fancy string concatenation or printf like
formatting. In fact, hard coding query parameters in the query string is a
poor (and can even be risky) practice. 

Agreed that table name itself as a parameter is not liked by some databases
and one can make a concession on that. 

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/java-lang-IllegalArgumentException-no-named-params-tp5080072p5098951.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: java.lang.IllegalArgumentException: no-named-params

Posted by Daryl Stultz <da...@opentempo.com>.
On Fri, May 21, 2010 at 1:32 PM, C N Davies <cn...@cndavies.com> wrote:

> Avoidance of NPE :)
>
> String.format("SELECT id FROM %s WHERE name like '%s')", "monkey", "doo")
>
> I'm stumped.
>
> At first I was trying to be funny, but then I realized I actually don't
know how to solve the problem (how write a LIKE pattern with % without it
breaking String.format()). I don't use String.format() at all. The query is
supposed to match Monkeys that have names that end with "doo". Two % signs
don't form the right string.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
http://www.opentempo.com
mailto:daryl.stultz@opentempo.com

RE: java.lang.IllegalArgumentException: no-named-params

Posted by C N Davies <cn...@cndavies.com>.
Avoidance of NPE :)


-----Original Message-----
From: Daryl Stultz [mailto:daryl.stultz@opentempo.com] 
Sent: Saturday, 22 May 2010 1:32 AM
To: users@openjpa.apache.org
Subject: Re: java.lang.IllegalArgumentException: no-named-params

On Fri, May 21, 2010 at 11:22 AM, C N Davies <cn...@cndavies.com> wrote:

> I don't mean to be pedantic, but
>
> String.format("SELECT id FROM %s WHERE (next_run - now() < interval '2
> minutes')", tablename);
>
> Hmmm:

String.format("SELECT id FROM %s WHERE name like '%s')", "monkey", "doo")

I'm stumped.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
http://www.opentempo.com
mailto:daryl.stultz@opentempo.com


Re: java.lang.IllegalArgumentException: no-named-params

Posted by Daryl Stultz <da...@opentempo.com>.
On Fri, May 21, 2010 at 11:22 AM, C N Davies <cn...@cndavies.com> wrote:

> I don't mean to be pedantic, but
>
> String.format("SELECT id FROM %s WHERE (next_run - now() < interval '2
> minutes')", tablename);
>
> Hmmm:

String.format("SELECT id FROM %s WHERE name like '%s')", "monkey", "doo")

I'm stumped.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
http://www.opentempo.com
mailto:daryl.stultz@opentempo.com

RE: java.lang.IllegalArgumentException: no-named-params

Posted by C N Davies <cn...@cndavies.com>.
I don't mean to be pedantic, but 

String.format("SELECT id FROM %s WHERE (next_run - now() < interval '2 minutes')", tablename);

Chris


-----Original Message-----
From: Miłosz [mailto:mtylenda@o2.pl] 
Sent: Saturday, 22 May 2010 12:11 AM
To: users@openjpa.apache.org
Subject: Re: java.lang.IllegalArgumentException: no-named-params

Hi Juraj,

The main problem here is that databases don't like providing table names as parameters. If you want to use different tables in a query, you have to concatenate strings:

"SELECT id FROM " + tableName + " WHERE (next_run - now() < interval '2 minutes')"

Cheers,
Milosz


> thanks for reply, but your suggested code:
> 
> 
> Rick Curtis wrote:
> > 
> > Query query = em.createNativeQuery("SELECT id FROM :?1 WHERE (next_run -
> > now() < interval '2 minutes')", Long.class);
> > query.setParameter(1, HARVEST_SCHEDULES_TABLE);
> > 
> 
> produces systax error: (i don't know why the exception sais :? and not :?1 ,
> weird)
> 
> 
> 
> >  ERROR: syntax error at or near ":"
> >   Position: 16 {prepstmnt 9497985 SELECT id FROM :? WHERE (next_run -
> > now() < interval '2 minutes') 
> > 
> 
> i tried to change it to this:
> 
> 
> > Query query = em
> > 				.createNativeQuery(
> > 						"SELECT id FROM ?1 WHERE (next_run - now() < interval '2 minutes')",
> > 						Long.class);
> > 
> 
> but its also not working:
> 
> 
> 
> > ERROR: syntax error at or near "$1"
> >   Position: 16 {prepstmnt 8172621 SELECT id FROM ? WHERE (next_run - now()
> > < interval '2 minutes')
> > 
> 
> thanks for some suggestion, Juraj
> 
> -- 
> View this message in context: http://openjpa.208410.n2.nabble.com/java-lang-IllegalArgumentException-no-named-params-tp5080072p5083226.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: java.lang.IllegalArgumentException: no-named-params

Posted by Miłosz <mt...@o2.pl>.
Hi Juraj,

The main problem here is that databases don't like providing table names as parameters. If you want to use different tables in a query, you have to concatenate strings:

"SELECT id FROM " + tableName + " WHERE (next_run - now() < interval '2 minutes')"

Cheers,
Milosz


> thanks for reply, but your suggested code:
> 
> 
> Rick Curtis wrote:
> > 
> > Query query = em.createNativeQuery("SELECT id FROM :?1 WHERE (next_run -
> > now() < interval '2 minutes')", Long.class);
> > query.setParameter(1, HARVEST_SCHEDULES_TABLE);
> > 
> 
> produces systax error: (i don't know why the exception sais :? and not :?1 ,
> weird)
> 
> 
> 
> >  ERROR: syntax error at or near ":"
> >   Position: 16 {prepstmnt 9497985 SELECT id FROM :? WHERE (next_run -
> > now() < interval '2 minutes') 
> > 
> 
> i tried to change it to this:
> 
> 
> > Query query = em
> > 				.createNativeQuery(
> > 						"SELECT id FROM ?1 WHERE (next_run - now() < interval '2 minutes')",
> > 						Long.class);
> > 
> 
> but its also not working:
> 
> 
> 
> > ERROR: syntax error at or near "$1"
> >   Position: 16 {prepstmnt 8172621 SELECT id FROM ? WHERE (next_run - now()
> > < interval '2 minutes')
> > 
> 
> thanks for some suggestion, Juraj
> 
> -- 
> View this message in context: http://openjpa.208410.n2.nabble.com/java-lang-IllegalArgumentException-no-named-params-tp5080072p5083226.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: java.lang.IllegalArgumentException: no-named-params

Posted by duro <ju...@gmail.com>.
thanks for reply, but your suggested code:


Rick Curtis wrote:
> 
> Query query = em.createNativeQuery("SELECT id FROM :?1 WHERE (next_run -
> now() < interval '2 minutes')", Long.class);
> query.setParameter(1, HARVEST_SCHEDULES_TABLE);
> 

produces systax error: (i don't know why the exception sais :? and not :?1 ,
weird)



>  ERROR: syntax error at or near ":"
>   Position: 16 {prepstmnt 9497985 SELECT id FROM :? WHERE (next_run -
> now() < interval '2 minutes') 
> 

i tried to change it to this:


> Query query = em
> 				.createNativeQuery(
> 						"SELECT id FROM ?1 WHERE (next_run - now() < interval '2 minutes')",
> 						Long.class);
> 

but its also not working:



> ERROR: syntax error at or near "$1"
>   Position: 16 {prepstmnt 8172621 SELECT id FROM ? WHERE (next_run - now()
> < interval '2 minutes')
> 

thanks for some suggestion, Juraj

-- 
View this message in context: http://openjpa.208410.n2.nabble.com/java-lang-IllegalArgumentException-no-named-params-tp5080072p5083226.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: java.lang.IllegalArgumentException: no-named-params

Posted by Rick Curtis <cu...@gmail.com>.
With native queries you need to use indexes for parameters. I'm not sure why
the message "no-named-params" wasn't translated into the proper message...
I'll try to take a look at that when I get a chance.

Query query = em.createNativeQuery("SELECT id FROM :?1 WHERE (next_run -
now() < interval '2 minutes')", Long.class);
query.setParameter(1, HARVEST_SCHEDULES_TABLE);

Thanks,
Rick

On Thu, May 20, 2010 at 10:00 AM, duro <ju...@gmail.com> wrote:

>
> Hello, when i try to set parameter to a query like this:
> Query query = em
>                                .createNativeQuery(
>                                                "SELECT id FROM :table WHERE
> (next_run - now() < interval '2
> minutes')",
>                                                Long.class);
>                query.setParameter("table", HARVEST_SCHEDULES_TABLE);
>
> i get this error:
>
> Exception in thread "Thread-1" java.lang.IllegalArgumentException:
> no-named-params
>        at
> org.apache.openjpa.persistence.QueryImpl.setParameter(QueryImpl.java:1066)
>        at
> org.apache.openjpa.persistence.QueryImpl.setParameter(QueryImpl.java:79)
>
> what am i doing wrong? thanks a lot
>
> ps: using openjpa 2.0.0
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/java-lang-IllegalArgumentException-no-named-params-tp5080072p5080072.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>