[
https://issues.apache.org/jira/browse/OPENJPA-1992?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13032724#comment-13032724
]
David Blevins commented on OPENJPA-1992:
----------------------------------------
>From what I recall from EJB 3.0 when that group still had JPA, is that the
>intent of "input parameters are numbered starting from 1" is to simply say 1
>as opposed to 0. The line that reads "the same parameter can be used more
>than once in the query string", should simply read "the same parameter can be
>used zero or more times in the query string". That was the intent as far as I
>recall.
Not even printf requires you to use all your input parameters. It's ok to skip
some, even the first one. The 'start at 1' is for the input side, not the
output side (in this case the query itself)
For example:
{code}
public class PositionalParametersTest extends TestCase {
public void testPattern() throws Exception {
final String query = "SELECT $2 FROM $3 AS $2";
final String result = "one two three four".replaceFirst("(one) (two)
(three) (four)", query);
assertEquals("SELECT two FROM three AS two", result);
}
public void testFormatter() throws Exception {
final String query = "SELECT %2$s FROM %3$s AS %2$s";
final String result = String.format(query, "one", "two", "three",
"four");
assertEquals("SELECT two FROM three AS two", result);
}
}
{code}
It is very very useful to be able to edit any part of a query string while
trying to get things right or debugging. One of those things you don't do
because you want to, but because you're trying to get to where you want to go.
Being able to tweak query strings without recompiling code is a pretty useful
thing all on its own.
> java.lang.ArrayIndexOutOfBoundsException if positional parameter are not
> started from 1
> ---------------------------------------------------------------------------------------
>
> Key: OPENJPA-1992
> URL: https://issues.apache.org/jira/browse/OPENJPA-1992
> Project: OpenJPA
> Issue Type: Bug
> Components: kernel
> Affects Versions: 2.1.0
> Reporter: Han Hong Fang
> Assignee: Michael Dick
> Fix For: 2.2.0
>
> Attachments: Main.java, OPENJPA-1992.patch
>
>
> Query q = em.createQuery("SELECT x FROM Magazine x WHERE x.title = ?2 and
> x.price > ?3");
> q.setParameter(2, "JDJ").setParameter(3, 25.0);
> When the query is executed, java.lang.ArrayIndexOutOfBoundsException will be
> thrown. Since "JDJ" and "25.0" will be stored in arr[0] and arr[1] (when
> arr.size =2) but the query execution code tries to get them via arr[2] and
> arr[3].
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira