Skip to content

Commit 2fc51bd

Browse files
committed
attempt to untangle some convoluted logic in Query hierarchy
1 parent 52e185b commit 2fc51bd

File tree

5 files changed

+65
-168
lines changed

5 files changed

+65
-168
lines changed

hibernate-core/src/main/java/org/hibernate/query/spi/AbstractCommonQueryContract.java

Lines changed: 30 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.Map;
1616
import java.util.Set;
1717

18-
import org.hibernate.CacheMode;
1918
import org.hibernate.FlushMode;
2019
import org.hibernate.query.QueryFlushMode;
2120
import org.hibernate.HibernateException;
@@ -40,8 +39,6 @@
4039
import org.hibernate.query.CommonQueryContract;
4140
import org.hibernate.query.QueryLogging;
4241
import org.hibernate.query.QueryParameter;
43-
import org.hibernate.query.ResultListTransformer;
44-
import org.hibernate.query.TupleTransformer;
4542
import org.hibernate.query.TypedParameterValue;
4643
import org.hibernate.query.criteria.JpaExpression;
4744
import org.hibernate.query.internal.QueryOptionsImpl;
@@ -56,7 +53,6 @@
5653
import jakarta.persistence.CacheRetrieveMode;
5754
import jakarta.persistence.CacheStoreMode;
5855
import jakarta.persistence.EntityGraph;
59-
import jakarta.persistence.FlushModeType;
6056
import jakarta.persistence.LockModeType;
6157
import jakarta.persistence.Parameter;
6258
import jakarta.persistence.TemporalType;
@@ -145,15 +141,11 @@ else if ( expression instanceof SqmParameter<?> ) {
145141
throw new IllegalArgumentException( "Can't get max rows value from: " + expression );
146142
}
147143
// Note that we can never have ties because this is only used when we de-duplicate results
148-
switch ( selectStatement.getFetchClauseType() ) {
149-
case ROWS_ONLY:
150-
case ROWS_WITH_TIES:
151-
return fetchValue.intValue();
152-
case PERCENT_ONLY:
153-
case PERCENT_WITH_TIES:
154-
return (int) Math.ceil( ( ( (double) size ) * fetchValue.doubleValue() ) / 100d );
155-
}
156-
throw new UnsupportedOperationException( "Unsupported fetch clause type: " + selectStatement.getFetchClauseType() );
144+
return switch ( selectStatement.getFetchClauseType() ) {
145+
case ROWS_ONLY, ROWS_WITH_TIES -> fetchValue.intValue();
146+
case PERCENT_ONLY, PERCENT_WITH_TIES ->
147+
(int) Math.ceil( ( ((double) size) * fetchValue.doubleValue() ) / 100d );
148+
};
157149
}
158150

159151

@@ -244,13 +236,14 @@ protected void putIfNotNull(Map<String, Object> hints, String hintName, Object h
244236

245237
@Override
246238
public CommonQueryContract setHint(String hintName, Object value) {
247-
applyHint( hintName, value );
239+
if ( !applyHint( hintName, value ) ) {
240+
QueryLogging.QUERY_MESSAGE_LOGGER.ignoringUnrecognizedQueryHint( hintName );
241+
}
248242
return this;
249243
}
250244

251245
public final boolean applyHint(String hintName, Object value) {
252246
getSession().checkOpen( true );
253-
254247
try {
255248
switch ( hintName ) {
256249
case HINT_FLUSH_MODE:
@@ -277,13 +270,7 @@ public final boolean applyHint(String hintName, Object value) {
277270
applyDatabaseHint( (String) value );
278271
return true;
279272
default:
280-
if ( applySelectionHint( hintName, value ) || applyAdditionalPossibleHints( hintName, value ) ) {
281-
return true;
282-
}
283-
else {
284-
QueryLogging.QUERY_MESSAGE_LOGGER.ignoringUnrecognizedQueryHint( hintName );
285-
return false;
286-
}
273+
return applySelectionHint( hintName, value );
287274
}
288275
}
289276
catch ( ClassCastException e ) {
@@ -300,36 +287,41 @@ protected final boolean applySelectionHint(String hintName, Object value) {
300287
return true;
301288
}
302289
else {
290+
final MutableQueryOptions queryOptions = getQueryOptions();
303291
switch ( hintName ) {
304292
case HINT_READONLY:
305-
applyReadOnlyHint( getBoolean( value ) );
293+
queryOptions.setReadOnly( getBoolean( value ) );
306294
return true;
307295
case HINT_FETCH_SIZE:
308-
applyFetchSizeHint( getInteger( value ) );
296+
queryOptions.setFetchSize( getInteger( value ) );
309297
return true;
310298
case HINT_QUERY_PLAN_CACHEABLE:
311-
applyQueryPlanCacheableHint( getBoolean( value ) );
299+
queryOptions.setQueryPlanCachingEnabled( getBoolean( value ) );
312300
return true;
313301
case HINT_CACHEABLE:
314-
applyCacheableHint( getBoolean( value ) );
302+
queryOptions.setResultCachingEnabled( getBoolean( value ) );
315303
return true;
316304
case HINT_CACHE_REGION:
317-
applyCacheRegionHint( (String) value );
305+
queryOptions.setResultCacheRegionName( (String) value );
318306
return true;
319307
case HINT_CACHE_MODE:
320-
applyCacheModeHint( getCacheMode( value ) );
308+
queryOptions.setCacheMode( getCacheMode( value ) );
321309
return true;
322310
case HINT_JAVAEE_CACHE_RETRIEVE_MODE:
323311
DEPRECATION_LOGGER.deprecatedSetting( HINT_JAVAEE_CACHE_RETRIEVE_MODE, HINT_SPEC_CACHE_RETRIEVE_MODE );
324312
//fall through to:
325313
case HINT_SPEC_CACHE_RETRIEVE_MODE:
326-
applyJpaCacheRetrieveModeHint( value != null ? CacheRetrieveMode.valueOf( value.toString() ) : null );
314+
final CacheRetrieveMode retrieveMode =
315+
value == null ? null : CacheRetrieveMode.valueOf( value.toString() );
316+
queryOptions.setCacheRetrieveMode( retrieveMode );
327317
return true;
328318
case HINT_JAVAEE_CACHE_STORE_MODE:
329319
DEPRECATION_LOGGER.deprecatedSetting( HINT_JAVAEE_CACHE_STORE_MODE, HINT_SPEC_CACHE_STORE_MODE );
330320
//fall through to:
331321
case HINT_SPEC_CACHE_STORE_MODE:
332-
applyJpaCacheStoreModeHint( value != null ? CacheStoreMode.valueOf( value.toString() ) : null );
322+
final CacheStoreMode storeMode =
323+
value == null ? null : CacheStoreMode.valueOf( value.toString() );
324+
queryOptions.setCacheStoreMode( storeMode );
333325
return true;
334326
case HINT_JAVAEE_FETCH_GRAPH:
335327
DEPRECATION_LOGGER.deprecatedSetting( HINT_JAVAEE_FETCH_GRAPH, HINT_SPEC_FETCH_GRAPH );
@@ -350,37 +342,13 @@ protected final boolean applySelectionHint(String hintName, Object value) {
350342
}
351343
}
352344

353-
protected void applyFetchSizeHint(int fetchSize) {
354-
getQueryOptions().setFetchSize( fetchSize );
355-
}
356-
357-
protected void applyQueryPlanCacheableHint(boolean isCacheable) {
358-
getQueryOptions().setQueryPlanCachingEnabled( isCacheable );
359-
}
360-
361-
protected void applyCacheModeHint(CacheMode cacheMode) {
362-
getQueryOptions().setCacheMode( cacheMode );
363-
}
364-
365-
protected void applyCacheableHint(boolean isCacheable) {
366-
getQueryOptions().setResultCachingEnabled( isCacheable );
367-
}
368-
369-
protected void applyCacheRegionHint(String regionName) {
370-
getQueryOptions().setResultCacheRegionName( regionName );
371-
}
372-
373-
private void applyReadOnlyHint(Boolean readOnly) {
374-
getQueryOptions().setReadOnly( readOnly );
375-
}
376-
377345
protected void applyEntityGraphHint(String hintName, Object value) {
378346
final GraphSemantic graphSemantic = GraphSemantic.fromHintName( hintName );
379-
if ( value instanceof RootGraphImplementor ) {
380-
applyGraph( (RootGraphImplementor<?>) value, graphSemantic );
347+
if ( value instanceof RootGraphImplementor<?> rootGraphImplementor ) {
348+
applyGraph( rootGraphImplementor, graphSemantic );
381349
}
382-
else if ( value instanceof String ) {
383-
applyGraph( (String) value, graphSemantic );
350+
else if ( value instanceof String string ) {
351+
applyGraph( string, graphSemantic );
384352
}
385353
else {
386354
throw new IllegalArgumentException( "The value of the hint '" + hintName
@@ -465,11 +433,11 @@ protected void applyLockModeType(LockModeType value) {
465433
}
466434

467435
protected final void applyLockModeHint(Object value) {
468-
if ( value instanceof LockMode ) {
469-
applyHibernateLockMode( (LockMode) value );
436+
if ( value instanceof LockMode lockMode ) {
437+
applyHibernateLockMode( lockMode );
470438
}
471-
else if ( value instanceof LockModeType ) {
472-
applyLockModeType( (LockModeType) value );
439+
else if ( value instanceof LockModeType lockModeType ) {
440+
applyLockModeType( lockModeType );
473441
}
474442
else if ( value instanceof String ) {
475443
applyHibernateLockMode( interpretLockMode( value ) );
@@ -503,10 +471,6 @@ protected void applyFollowOnLockingHint(Boolean followOnLocking) {
503471
getQueryOptions().getLockOptions().setFollowOnLocking( followOnLocking );
504472
}
505473

506-
protected boolean applyAdditionalPossibleHints(String hintName, Object value) {
507-
return false;
508-
}
509-
510474

511475
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512476
// Options
@@ -543,16 +507,6 @@ public CommonQueryContract setQueryFlushMode(QueryFlushMode queryFlushMode) {
543507
return this;
544508
}
545509

546-
protected boolean applyJpaCacheRetrieveModeHint(CacheRetrieveMode retrieveMode) {
547-
getQueryOptions().setCacheRetrieveMode( retrieveMode );
548-
return true;
549-
}
550-
551-
protected boolean applyJpaCacheStoreModeHint(CacheStoreMode storeMode) {
552-
getQueryOptions().setCacheStoreMode( storeMode );
553-
return true;
554-
}
555-
556510
protected void applyTimeoutHint(int timeout) {
557511
setTimeout( timeout );
558512
}
@@ -593,51 +547,11 @@ public int getMaxResults() {
593547
return getQueryOptions().getLimit().getMaxRowsJpa();
594548
}
595549

596-
public void applyMaxResults(int maxResult) {
597-
if ( maxResult < 0 ) {
598-
throw new IllegalArgumentException( "max-results cannot be negative" );
599-
}
600-
getSession().checkOpen();
601-
getQueryOptions().getLimit().setMaxRows( maxResult );
602-
}
603-
604550
public int getFirstResult() {
605551
getSession().checkOpen();
606552
return getQueryOptions().getLimit().getFirstRowJpa();
607553
}
608554

609-
public void applyFirstResult(int startPosition) {
610-
if ( startPosition < 0 ) {
611-
throw new IllegalArgumentException( "first-result value cannot be negative : " + startPosition );
612-
}
613-
614-
getSession().checkOpen();
615-
getQueryOptions().getLimit().setFirstRow( startPosition );
616-
}
617-
618-
protected FlushModeType getJpaFlushMode() {
619-
getSession().checkOpen();
620-
final FlushMode flushMode = getQueryOptions().getFlushMode() == null
621-
? getSession().getHibernateFlushMode()
622-
: getQueryOptions().getFlushMode();
623-
return FlushModeTypeHelper.getFlushModeType( flushMode );
624-
}
625-
626-
protected void applyJpaFlushMode(FlushModeType flushModeType) {
627-
getSession().checkOpen();
628-
setHibernateFlushMode( FlushModeTypeHelper.getFlushMode( flushModeType ) );
629-
}
630-
631-
public boolean applyTupleTransformer(TupleTransformer<?> transformer) {
632-
getQueryOptions().setTupleTransformer( transformer );
633-
return true;
634-
}
635-
636-
public boolean applyResultListTransformer(ResultListTransformer<?> transformer) {
637-
getQueryOptions().setResultListTransformer( transformer );
638-
return true;
639-
}
640-
641555

642556
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
643557
// Parameter handling

hibernate-core/src/main/java/org/hibernate/query/spi/AbstractQuery.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.hibernate.internal.CoreLogging;
3737
import org.hibernate.internal.CoreMessageLogger;
3838
import org.hibernate.jpa.AvailableHints;
39-
import org.hibernate.jpa.internal.util.FlushModeTypeHelper;
4039
import org.hibernate.jpa.internal.util.LockModeTypeHelper;
4140
import org.hibernate.query.BindableType;
4241
import org.hibernate.query.IllegalQueryOperationException;
@@ -197,20 +196,13 @@ public QueryImplementor<R> setHibernateFlushMode(FlushMode flushMode) {
197196

198197
@Override
199198
public QueryImplementor<R> setQueryFlushMode(QueryFlushMode queryFlushMode) {
200-
super.setQueryFlushMode(queryFlushMode);
199+
super.setQueryFlushMode( queryFlushMode );
201200
return this;
202201
}
203202

204-
@Override
205-
public FlushModeType getFlushMode() {
206-
// getSession().checkOpen();
207-
return FlushModeTypeHelper.getFlushModeType( getHibernateFlushMode() );
208-
}
209-
210203
@Override
211204
public QueryImplementor<R> setFlushMode(FlushModeType flushModeType) {
212-
// getSession().checkOpen();
213-
setHibernateFlushMode( FlushModeTypeHelper.getFlushMode( flushModeType ) );
205+
super.setFlushMode( flushModeType );
214206
return this;
215207
}
216208

@@ -400,7 +392,6 @@ protected boolean resolveJdbcParameterTypeIfNecessary() {
400392
}
401393

402394
@Override
403-
@SuppressWarnings( {"unchecked", "rawtypes"} )
404395
public Set<Parameter<?>> getParameters() {
405396
return super.getParameters();
406397
}

hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,26 +334,32 @@ protected void resetCallback() {
334334

335335
@Override
336336
public FlushModeType getFlushMode() {
337+
getSession().checkOpen();
337338
return FlushModeTypeHelper.getFlushModeType( getHibernateFlushMode() );
338339
}
339340

340341
@Override
341342
public SelectionQuery<R> setFlushMode(FlushModeType flushMode) {
343+
getSession().checkOpen();
342344
getQueryOptions().setFlushMode( FlushModeTypeHelper.getFlushMode( flushMode ) );
343345
return this;
344346
}
345347

346348
@Override
347349
public SelectionQuery<R> setMaxResults(int maxResult) {
348-
super.applyMaxResults( maxResult );
350+
if ( maxResult < 0 ) {
351+
throw new IllegalArgumentException( "Max results cannot be negative" );
352+
}
353+
getSession().checkOpen();
354+
getQueryOptions().getLimit().setMaxRows(maxResult);
349355
return this;
350356
}
351357

352358
@Override
353359
public SelectionQuery<R> setFirstResult(int startPosition) {
354360
getSession().checkOpen();
355361
if ( startPosition < 0 ) {
356-
throw new IllegalArgumentException( "first-result value cannot be negative : " + startPosition );
362+
throw new IllegalArgumentException( "First result cannot be negative" );
357363
}
358364
getQueryOptions().getLimit().setFirstRow( startPosition );
359365
return this;

0 commit comments

Comments
 (0)