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. :)