-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Different handling of positional parameters in HQL and SQL?
PostPosted: Sun Aug 31, 2014 10:00 am 
Newbie

Joined: Sun Aug 31, 2014 9:44 am
Posts: 1
Location: Germany
Hello everyone, :)

I came across an interesting behaviour of Hibernate 4.3.5 running on Java 1.7.0 Update 67 on Windows 7 64 Bit.
If I have a HQL query like this:
Code:
FROM bill WHERE (customerNumber = ?1)

everything works fine. But if I have a SQL query like this
Code:
SELECT COUNT(id) FROM bill WHERE (customerNumber = ?1)

I get an error when I try to set the parameter
Code:
org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
   at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:89)
   at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:109)
   at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:507)
   at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:479)
   [...]

If I change ?1 to ? in both queries, the SQL works fine, but I get the following error from the HQL query:
Code:
org.hibernate.QueryParameterException: could not locate named parameter [1]
   at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:148)
   at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:165)
   at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:523)
   at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:493)
   [...]

Beside the fact that it is not recommended to use positional parameters I'm wondering why Hibernate treats position parameters different in HQL and SQL. I even can't remember from where I get that ?1 version because it is not mentioned in the documentation. Maybe it was in one of the previous versions.

It would be nice and I would very thankful if someone could shed light on this. :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.