[hibernate-issues] [Hibernate-JIRA] Commented: (EJB-214) Native Query can not be used with parameter

Steve Ebersole (JIRA) noreply at atlassian.com
Wed Aug 30 09:16:24 EDT 2006


    [ http://opensource.atlassian.com/projects/hibernate/browse/EJB-214?page=comments#action_24233 ] 

Steve Ebersole commented on EJB-214:
------------------------------------

The underlying cause is in o.h.e.QueryImpl in the public Query setParameter(int position, Object value) implementation.  Specifically, the check: if ( isEJBQLQuery() )...  Essentially, if this is an ~HQL query, the parameter is correctly converted to a Hibernate named parameter.  However, in this case, we are talking about a native-sql query; here, the isEJBQLQuery() check evaluates false, and so the parameter is passed on to the underlying Hibernate query object as a ordinal parameter instead.  What was the reason for the distinction here?

There is most definitely a mismatch here.  Hibernate core treats all "?n" (where n is a numeric) sequences as a variation of named parameters...

> Native Query can not be used with parameter
> -------------------------------------------
>
>          Key: EJB-214
>          URL: http://opensource.atlassian.com/projects/hibernate/browse/EJB-214
>      Project: Hibernate Entity Manager
>         Type: Bug

>   Components: EntityManager
>     Versions: 3.2.0.cr1
>  Environment: Hibernate EntityManager 3.2.0.cr1
> Hibernate Core 3.2.0 cr2
>     Reporter: Shinpei Ohtani
>     Assignee: Emmanuel Bernard
>      Fix For: 3.2.0.cr2

>
>
> When using Native Query with parameter, IndexOutOfBoundsException is occurred.
> org.hibernate.engine.query.ParameterParser(Hibernate Core) handles positional parameter as named parameter internally,
> but org.hibernate.ejb.QueryImpl(Hibernate EntityManager) expects positional(ordinal) parameter, not named parameter.
> Here is sample for this issue.
> =======
> Query query = em.createNativeQuery("SELECT d.name FROM Department d WHERE d.id = ?1");
> query.setParameter(1, 1);
> =======
> Result is
> java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
>        at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:55)
>        at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:61)
>        at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:389)
>        at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:369)
>        at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:198)

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira




More information about the hibernate-issues mailing list