You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Reddy SMK <sm...@gmail.com> on 2007/06/04 21:21:06 UTC

getPositionalParameters/getNamedParameters/hasPositionalParameters behavior

Hi Guys

i tried these method for some reason i am not getting the expected
results.... has any one tried these methods? i have the following named
query...

SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = :personName,
when i use the getNamedParameters on the it returns an empty map...


            Query query = em.createNamedQuery(jpql);
            OpenJPAQuery jpaQuery = OpenJPAPersistence.cast(query);
            if (jpaQuery.hasPositionalParameters())
                throw new QueryException(
                        "The query does not has named parameters please
check your arguments");

            Map namedParamMap = (Map) jpaQuery.getNamedParameters();


in this case, based on the documentation, should expect a map with one
entry.. but for some reason i am getting null map..

same for the positional args
SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = ?1, when i
use the getPositionalParameters on the it returns an empty map...

           Query query = em.createNamedQuery(jpql);
            OpenJPAQuery jpaQuery = OpenJPAPersistence.cast(query);
            jpaQuery.compile();
            if (!jpaQuery.hasPositionalParameters())
                throw new QueryException(
                        "The query does not has positional parameters please
check your arguments");
            Object[] positionParamArray = (Object[]) jpaQuery
                    .getPositionalParameters();


I am not sure if my understanding is wrong or the methods are not behaving
appropriately...

Re: getPositionalParameters/getNamedParameters/hasPositionalParameters behavior

Posted by Patrick Linskey <pl...@gmail.com>.
I believe that it's only meaningful to invoke that method after having
set parameters. So, that method is not there for introspection of the
query definition, but rather for obtaining parameters set via
setParameter(...).

If you want to get details about the declared parameters in the query
definition, you'll need to dig into the
org.apache.openjpa.kernel.Query that backs the query instance:

org.apache.openjpa.kernel.Query kernelQuery = ((QueryImpl) q).getDelegate();
Map params = kernelQuery.getParameterTypes();

Clearly, that's not part of the extension API, but it'll do the trick.

-Patrick

On 6/4/07, Reddy SMK <sm...@gmail.com> wrote:
> Hi Guys
>
> i tried these method for some reason i am not getting the expected
> results.... has any one tried these methods? i have the following named
> query...
>
> SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = :personName,
> when i use the getNamedParameters on the it returns an empty map...
>
>
>             Query query = em.createNamedQuery(jpql);
>             OpenJPAQuery jpaQuery = OpenJPAPersistence.cast(query);
>             if (jpaQuery.hasPositionalParameters())
>                 throw new QueryException(
>                         "The query does not has named parameters please
> check your arguments");
>
>             Map namedParamMap = (Map) jpaQuery.getNamedParameters();
>
>
> in this case, based on the documentation, should expect a map with one
> entry.. but for some reason i am getting null map..
>
> same for the positional args
> SELECT x FROM Magazine x, IN(x.articles) y WHERE y.authorName = ?1, when i
> use the getPositionalParameters on the it returns an empty map...
>
>            Query query = em.createNamedQuery(jpql);
>             OpenJPAQuery jpaQuery = OpenJPAPersistence.cast(query);
>             jpaQuery.compile();
>             if (!jpaQuery.hasPositionalParameters())
>                 throw new QueryException(
>                         "The query does not has positional parameters please
> check your arguments");
>             Object[] positionParamArray = (Object[]) jpaQuery
>                     .getPositionalParameters();
>
>
> I am not sure if my understanding is wrong or the methods are not behaving
> appropriately...
>


-- 
Patrick Linskey
202 669 5907