Zero based positional query parameters for legacy Session.createSQLQuery() doesnt work

Description

Following code should work but doesn't:
session.createSQLQuery("select some_property from some_table where other_property = ?").setParameter(0, "val");

Error:

"java.lang.IllegalArgumentException: Could not locate ordinal parameter [0], expecting one of [1]"

The query returned from hibernate specific createSQLQuery should expect zero based (backward compatibility) - rather than the JPA method createNativeQuery.

I dug the hibernate code and found that createNativeQuery expects 1-based (as expected), but legacy method createSQLQuery actually supports the legacy 0-based parameters (also as expected)

@Override
public NativeQueryImplementor createSQLQuery(String queryString) {
return getNativeQueryImplementor( queryString, true );
}

where true is for zero based params.


Problem rises deeper in the code in ParamLocationRecognizer, where this argument is ignored and instead a global jdbc property is used:

public static ParamLocationRecognizer parseLocations(
String query,
SessionFactoryImplementor sessionFactory) {
final ParamLocationRecognizer recognizer = new ParamLocationRecognizer(
sessionFactory.getSessionFactoryOptions().jdbcStyleParamsZeroBased() ? 0 : 1
);
ParameterParser.parse( query, recognizer );
return recognizer;
}

This is critical for our project, we want to migrate from 4.3.8 to 5.4.2 and we unfortunately have lots of legacy queries - all of them now won't work until this bug is fixed

Attached is simple test case

Environment

Windows10, Hibernate 5.4.2

Status

Assignee

Unassigned

Reporter

dan pomerchik

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

4.3.8

Components

Affects versions

Priority

Major
Configure