Skip to content

Commit 763a70f

Browse files
committed
HHH-17314 - Add a configuration option to ignore jdbc parameters in native queries
Signed-off-by: Jan Schatteman <[email protected]>
1 parent ffee08d commit 763a70f

File tree

16 files changed

+220
-72
lines changed

16 files changed

+220
-72
lines changed

hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
248248
private TimeZone jdbcTimeZone;
249249
private final ValueHandlingMode criteriaValueHandlingMode;
250250
private final boolean criteriaCopyTreeEnabled;
251+
private final boolean nativeJdbcParametersIgnored;
251252
private final ImmutableEntityUpdateQueryHandlingMode immutableEntityUpdateQueryHandlingMode;
252253
// These two settings cannot be modified from the builder,
253254
// in order to maintain consistency.
@@ -551,6 +552,12 @@ else if ( jdbcTimeZoneValue != null ) {
551552
jpaBootstrap
552553
);
553554

555+
this.nativeJdbcParametersIgnored = getBoolean(
556+
AvailableSettings.NATIVE_IGNORE_JDBC_PARAMETERS,
557+
configurationSettings,
558+
false
559+
);
560+
554561
// added the boolean parameter in case we want to define some form of "all" as discussed
555562
this.jpaCompliance = context.getJpaCompliance();
556563

@@ -1145,6 +1152,11 @@ public boolean isCriteriaCopyTreeEnabled() {
11451152
return criteriaCopyTreeEnabled;
11461153
}
11471154

1155+
@Override
1156+
public boolean getNativeJdbcParametersIgnored() {
1157+
return nativeJdbcParametersIgnored;
1158+
}
1159+
11481160
@Override
11491161
public ImmutableEntityUpdateQueryHandlingMode getImmutableEntityUpdateQueryHandlingMode() {
11501162
return immutableEntityUpdateQueryHandlingMode;

hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryOptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ public boolean isCriteriaCopyTreeEnabled() {
382382
return delegate.isCriteriaCopyTreeEnabled();
383383
}
384384

385+
public boolean getNativeJdbcParametersIgnored() {
386+
return delegate.getNativeJdbcParametersIgnored();
387+
}
388+
385389
@Override
386390
public JpaCompliance getJpaCompliance() {
387391
return delegate.getJpaCompliance();

hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ default boolean isCriteriaCopyTreeEnabled() {
231231
return false;
232232
}
233233

234+
/**
235+
* @see org.hibernate.cfg.AvailableSettings#NATIVE_IGNORE_JDBC_PARAMETERS
236+
*/
237+
default boolean getNativeJdbcParametersIgnored() {
238+
return false;
239+
}
240+
234241
JpaCompliance getJpaCompliance();
235242

236243
boolean isFailOnPaginationOverCollectionFetchEnabled();

hibernate-core/src/main/java/org/hibernate/cfg/QuerySettings.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.hibernate.cfg;
88

9+
import org.hibernate.boot.spi.SessionFactoryOptions;
910
import org.hibernate.query.NullPrecedence;
1011
import org.hibernate.query.spi.QueryPlan;
1112

@@ -124,6 +125,16 @@ public interface QuerySettings {
124125
*/
125126
String CRITERIA_COPY_TREE = "hibernate.criteria.copy_tree";
126127

128+
/**
129+
* When set to true, indicates that ordinal parameters (represented by the '?' placeholder) in native queries will be ignored.
130+
* <p>
131+
* By default, this is set to false, i.e. native queries will be checked for ordinal placeholders.
132+
* <p>
133+
*
134+
* @see SessionFactoryOptions#getIgnoreNativeJdbcParameters()
135+
*/
136+
String NATIVE_IGNORE_JDBC_PARAMETERS = "hibernate.query.native.ignore_jdbc_parameters";
137+
127138
/**
128139
* When {@linkplain org.hibernate.query.Query#setMaxResults(int) pagination} is used
129140
* in combination with a {@code fetch join} applied to a collection or many-valued

hibernate-core/src/main/java/org/hibernate/engine/query/internal/NativeQueryInterpreterStandardImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ public class NativeQueryInterpreterStandardImpl implements NativeQueryInterprete
2121
/**
2222
* Singleton access
2323
*/
24-
public static final NativeQueryInterpreterStandardImpl NATIVE_QUERY_INTERPRETER = new NativeQueryInterpreterStandardImpl();
24+
public static final NativeQueryInterpreterStandardImpl NATIVE_QUERY_INTERPRETER = new NativeQueryInterpreterStandardImpl( false );
25+
26+
private boolean nativeJdbcParametersIgnored;
27+
28+
public NativeQueryInterpreterStandardImpl(boolean nativeJdbcParametersIgnored) {
29+
this.nativeJdbcParametersIgnored = nativeJdbcParametersIgnored;
30+
}
2531

2632
@Override
2733
public void recognizeParameters(String nativeQuery, ParameterRecognizer recognizer) {
28-
ParameterParser.parse( nativeQuery, recognizer );
34+
ParameterParser.parse( nativeQuery, recognizer, nativeJdbcParametersIgnored );
2935
}
3036

3137
@Override

hibernate-core/src/main/java/org/hibernate/engine/query/spi/NativeQueryInterpreterInitiator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class NativeQueryInterpreterInitiator implements SessionFactoryServiceIni
2121

2222
@Override
2323
public NativeQueryInterpreter initiateService(SessionFactoryServiceInitiatorContext context) {
24-
return new NativeQueryInterpreterStandardImpl();
24+
return new NativeQueryInterpreterStandardImpl( context.getSessionFactoryOptions().getNativeJdbcParametersIgnored() );
2525
}
2626

2727
@Override

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ public boolean isCriteriaCopyTreeEnabled() {
240240
return delegate.isCriteriaCopyTreeEnabled();
241241
}
242242

243+
@Override
244+
public boolean getNativeJdbcParametersIgnored() {
245+
return delegate.getNativeJdbcParametersIgnored();
246+
}
247+
248+
@Override
249+
public void setNativeJdbcParametersIgnored(boolean nativeJdbcParametersIgnored) {
250+
delegate.setNativeJdbcParametersIgnored( nativeJdbcParametersIgnored );
251+
}
252+
243253
@Override
244254
public boolean isOpen() {
245255
return delegate.isOpen();

hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ default String bestGuessEntityName(Object object, EntityEntry entry) {
381381

382382
boolean isCriteriaCopyTreeEnabled();
383383

384+
boolean getNativeJdbcParametersIgnored();
385+
386+
void setNativeJdbcParametersIgnored(boolean nativeJdbcParametersIgnored);
387+
384388
/**
385389
* Get the current {@link FlushModeType} for this session.
386390
* <p>

hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionDelegatorBaseImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,16 @@ public boolean isCriteriaCopyTreeEnabled() {
474474
return delegate.isCriteriaCopyTreeEnabled();
475475
}
476476

477+
@Override
478+
public boolean getNativeJdbcParametersIgnored() {
479+
return delegate.getNativeJdbcParametersIgnored();
480+
}
481+
482+
@Override
483+
public void setNativeJdbcParametersIgnored(boolean nativeJdbcParametersIgnored) {
484+
delegate.setNativeJdbcParametersIgnored( nativeJdbcParametersIgnored );
485+
}
486+
477487
@Override
478488
public FlushModeType getFlushMode() {
479489
return delegate.getFlushMode();

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
162162

163163
private boolean criteriaCopyTreeEnabled;
164164

165+
private boolean nativeJdbcParametersIgnored;
166+
165167
protected boolean closed;
166168
protected boolean waitingForAutoClose;
167169

@@ -183,6 +185,7 @@ public AbstractSharedSessionContract(SessionFactoryImpl factory, SessionCreation
183185
sessionEventsManager = createSessionEventsManager(options);
184186
entityNameResolver = new CoordinatingEntityNameResolver( factory, interceptor );
185187
setCriteriaCopyTreeEnabled( factory.getSessionFactoryOptions().isCriteriaCopyTreeEnabled() );
188+
setNativeJdbcParametersIgnored( factory.getSessionFactoryOptions().getNativeJdbcParametersIgnored() );
186189

187190
final StatementInspector statementInspector = interpret( options.getStatementInspector() );
188191

@@ -701,6 +704,16 @@ public boolean isCriteriaCopyTreeEnabled() {
701704
return criteriaCopyTreeEnabled;
702705
}
703706

707+
@Override
708+
public boolean getNativeJdbcParametersIgnored() {
709+
return nativeJdbcParametersIgnored;
710+
}
711+
712+
@Override
713+
public void setNativeJdbcParametersIgnored(boolean nativeJdbcParametersIgnored) {
714+
this.nativeJdbcParametersIgnored = nativeJdbcParametersIgnored;
715+
}
716+
704717
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
705718
// dynamic HQL handling
706719

0 commit comments

Comments
 (0)