NamedNativeQueries ordinal parameters are zero based

Description

This bug is the same as HHH-10885, except for NamedQueries and NamedNativeQueries instead of NativeQueries

The problem is that the position (ordinal paramer) in Query.setParameter(int, Object) is zero based. JPA 2.1 spec states that the ordinal parameters should be 1-based.

This works (but shouldn't)

This causes IllegalArgumentExceptions , but should work

A workaround is to use specific positions in the statement (e.g. ... where id = ?1 instead of ... where id = ? )

Arquillian test case is attached (can be run with mvn verify )

I believe this affects all Hibernate 5 versions (tested with 5.1.0.Final, 5.2.0.Final and 5.2.2.Final)

Environment

Java 1.8.0_74
Hibernate 5.2.2.Final
OSX 10.9 / Windows 7

Activity

Show:
Chris Cranford
September 8, 2016, 7:36 PM
Edited

Per JPA 2.1 specification (section 4.6.4.1), positional parameters are designated by a question mark prefix followed by an integer (e.g. ?1). So I would expect that the user's code should be using the proper JPA positional parameter notation in order to expect JPA compliant behavior.

Using the question mark notation designates the legacy Hibernate ordinal parameter placement which is and has always been 0-based.

The outcome observed is identical to what we see using `EntityManager#createQuery` using JPQL. Parameter bindings are built 0-based when using just the question mark syntax and built using 1-based when using JPA compliant question mark prefix followed by integer suffixes.

Chris Cranford
September 8, 2016, 7:48 PM

I also tested the user's attachment against 5.1.0.Final, 5.1.1.Final, and 5.0.10.Final and none of those builds have this issue. This is only observed with the 5.2.x release stream.

Gail Badner
September 8, 2016, 8:45 PM

, thanks for checking!

Steve Ebersole
September 8, 2016, 11:50 PM

You raise a good point that I missed originally wrt ? versus ?1. So to me this is "works as expected", no?

For what it is worth, we are dropping support for Hibernate's legacy positional parameters in 6.0...

Jerrimiah Nance
September 9, 2016, 8:48 AM
Edited

You are correct that this only affects the 5.2.x release stream. I do not know what I did wrong previously, but I am sorry for any confusion I may have caused by stating that it affects previous versions.
As for the the section 4.6.4.1 of the JPA spec, you are also correct - but only for JPQL. For NamedNativeQueries the bug still stands. (See section 3.10.13 of the spec:

It is assumed that for native queries the parameters themselves use the SQL syntax (i.e., “?”, rather than “?1”).

)

This means that this bug is smaller in scope than I previously thought. But using 0-based for NamedNativeQueries is definitely incorrect.

Assignee

Andrea Boriero

Reporter

Jerrimiah Nance

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

Priority

Critical
Configure