Skip to content

Commit 9a7b95b

Browse files
committed
misc code cleanups
1 parent d23dff8 commit 9a7b95b

File tree

3 files changed

+34
-52
lines changed

3 files changed

+34
-52
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public abstract class AbstractSelectionQuery<R>
7575
*/
7676
public static final String CRITERIA_HQL_STRING = "<criteria>";
7777

78-
private Callback callback;
78+
private transient Callback callback;
7979

8080
public AbstractSelectionQuery(SharedSessionContractImplementor session) {
8181
super( session );
@@ -270,9 +270,7 @@ public R getSingleResult() {
270270
try {
271271
final List<R> list = list();
272272
if ( list.isEmpty() ) {
273-
throw new NoResultException(
274-
String.format( "No result found for query [%s]", getQueryString() )
275-
);
273+
throw new NoResultException( "No result found for query [" + getQueryString() + "]" );
276274
}
277275
return uniqueElement( list );
278276
}

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
3636
import org.hibernate.query.sqm.tree.select.SqmSelection;
3737
import org.hibernate.sql.ast.SqlAstTranslator;
38-
import org.hibernate.sql.ast.spi.FromClauseAccess;
3938
import org.hibernate.sql.ast.tree.expression.Expression;
4039
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
4140
import org.hibernate.sql.ast.tree.expression.Literal;
@@ -61,7 +60,9 @@
6160
import static java.util.Collections.emptyList;
6261
import static org.hibernate.internal.util.ReflectHelper.isClass;
6362
import static org.hibernate.internal.util.collections.ArrayHelper.toStringArray;
63+
import static org.hibernate.query.sqm.internal.AppliedGraphs.containsCollectionFetches;
6464
import static org.hibernate.query.sqm.internal.QuerySqmImpl.CRITERIA_HQL_STRING;
65+
import static org.hibernate.query.sqm.internal.SqmUtil.generateJdbcParamsXref;
6566
import static org.hibernate.query.sqm.internal.SqmUtil.isSelectionAssignableToResultType;
6667

6768
/**
@@ -93,13 +94,10 @@ public ConcreteSqmSelectQueryPlan(
9394

9495
this.rowTransformer = determineRowTransformer( sqm, resultType, tupleMetadata, queryOptions );
9596

96-
final ListResultsConsumer.UniqueSemantic uniqueSemantic;
97-
if ( sqm.producesUniqueResults() && !AppliedGraphs.containsCollectionFetches( queryOptions ) ) {
98-
uniqueSemantic = ListResultsConsumer.UniqueSemantic.NONE;
99-
}
100-
else {
101-
uniqueSemantic = ListResultsConsumer.UniqueSemantic.ALLOW;
102-
}
97+
final ListResultsConsumer.UniqueSemantic uniqueSemantic =
98+
sqm.producesUniqueResults() && !containsCollectionFetches( queryOptions )
99+
? ListResultsConsumer.UniqueSemantic.NONE
100+
: ListResultsConsumer.UniqueSemantic.ALLOW;
103101
this.executeQueryInterpreter = (resultsConsumer, executionContext, sqmInterpretation, jdbcParameterBindings) -> {
104102
final SharedSessionContractImplementor session = executionContext.getSession();
105103
final JdbcOperationQuerySelect jdbcSelect = sqmInterpretation.getJdbcSelect();
@@ -141,8 +139,9 @@ public ConcreteSqmSelectQueryPlan(
141139
jdbcParameterBindings
142140
);
143141
session.autoFlushIfRequired( jdbcSelect.getAffectedTableNames(), true );
144-
final Expression fetchExpression = sqmInterpretation.selectStatement.getQueryPart()
145-
.getFetchClauseExpression();
142+
final Expression fetchExpression =
143+
sqmInterpretation.selectStatement.getQueryPart()
144+
.getFetchClauseExpression();
146145
final int resultCountEstimate = fetchExpression != null
147146
? interpretIntExpression( fetchExpression, jdbcParameterBindings )
148147
: -1;
@@ -173,12 +172,12 @@ public ConcreteSqmSelectQueryPlan(
173172
// jdbcParameterBindings
174173
// );
175174

176-
final JdbcSelectExecutor jdbcSelectExecutor = session.getFactory()
177-
.getJdbcServices()
178-
.getJdbcSelectExecutor();
175+
final JdbcSelectExecutor jdbcSelectExecutor =
176+
session.getFactory().getJdbcServices().getJdbcSelectExecutor();
179177
session.autoFlushIfRequired( jdbcSelect.getAffectedTableNames(), true );
180-
final Expression fetchExpression = sqmInterpretation.selectStatement.getQueryPart()
181-
.getFetchClauseExpression();
178+
final Expression fetchExpression =
179+
sqmInterpretation.selectStatement.getQueryPart()
180+
.getFetchClauseExpression();
182181
final int resultCountEstimate = fetchExpression != null
183182
? interpretIntExpression( fetchExpression, jdbcParameterBindings )
184183
: -1;
@@ -217,17 +216,19 @@ protected static SqmJdbcExecutionContextAdapter listInterpreterExecutionContext(
217216
}
218217

219218
protected static int interpretIntExpression(Expression expression, JdbcParameterBindings jdbcParameterBindings) {
220-
if ( expression instanceof Literal ) {
221-
return ( (Number) ( (Literal) expression ).getLiteralValue() ).intValue();
219+
if ( expression instanceof Literal literal ) {
220+
return ( (Number) literal.getLiteralValue() ).intValue();
222221
}
223-
else if ( expression instanceof JdbcParameter ) {
224-
return (int) jdbcParameterBindings.getBinding( (JdbcParameter) expression ).getBindValue();
222+
else if ( expression instanceof JdbcParameter jdbcParameter ) {
223+
return (int) jdbcParameterBindings.getBinding( jdbcParameter ).getBindValue();
225224
}
226-
else if ( expression instanceof SqmParameterInterpretation ) {
227-
return (int) jdbcParameterBindings.getBinding( (JdbcParameter) ( (SqmParameterInterpretation) expression ).getResolvedExpression() )
228-
.getBindValue();
225+
else if ( expression instanceof SqmParameterInterpretation parameterInterpretation ) {
226+
final JdbcParameter jdbcParameter = (JdbcParameter) parameterInterpretation.getResolvedExpression();
227+
return (int) jdbcParameterBindings.getBinding( jdbcParameter ).getBindValue();
228+
}
229+
else {
230+
return -1;
229231
}
230-
return -1;
231232
}
232233

233234
private static List<SqmSelection<?>> selections(SqmSelectStatement<?> sqm) {
@@ -389,11 +390,7 @@ private <T, X> T withCacheableSqmInterpretation(DomainQueryExecutionContext exec
389390
synchronized ( this ) {
390391
localCopy = cacheableSqmInterpretation;
391392
if ( localCopy == null ) {
392-
localCopy = buildCacheableSqmInterpretation(
393-
sqm,
394-
domainParameterXref,
395-
executionContext
396-
);
393+
localCopy = buildCacheableSqmInterpretation( sqm, domainParameterXref, executionContext );
397394
jdbcParameterBindings = localCopy.firstParameterBindings;
398395
localCopy.firstParameterBindings = null;
399396
cacheableSqmInterpretation = localCopy;
@@ -407,11 +404,7 @@ private <T, X> T withCacheableSqmInterpretation(DomainQueryExecutionContext exec
407404
// If the translation depends on the limit or lock options, we have to rebuild the JdbcSelect
408405
// We could avoid this by putting the lock options into the cache key
409406
if ( !localCopy.jdbcSelect.isCompatibleWith( jdbcParameterBindings, executionContext.getQueryOptions() ) ) {
410-
localCopy = buildCacheableSqmInterpretation(
411-
sqm,
412-
domainParameterXref,
413-
executionContext
414-
);
407+
localCopy = buildCacheableSqmInterpretation( sqm, domainParameterXref, executionContext );
415408
jdbcParameterBindings = localCopy.firstParameterBindings;
416409
localCopy.firstParameterBindings = null;
417410
cacheableSqmInterpretation = localCopy;
@@ -428,11 +421,7 @@ private <T, X> T withCacheableSqmInterpretation(DomainQueryExecutionContext exec
428421
// If the translation depends on the limit or lock options, we have to rebuild the JdbcSelect
429422
// We could avoid this by putting the lock options into the cache key
430423
if ( !localCopy.jdbcSelect.isCompatibleWith( jdbcParameterBindings, executionContext.getQueryOptions() ) ) {
431-
localCopy = buildCacheableSqmInterpretation(
432-
sqm,
433-
domainParameterXref,
434-
executionContext
435-
);
424+
localCopy = buildCacheableSqmInterpretation( sqm, domainParameterXref, executionContext );
436425
jdbcParameterBindings = localCopy.firstParameterBindings;
437426
localCopy.firstParameterBindings = null;
438427
cacheableSqmInterpretation = localCopy;
@@ -482,14 +471,12 @@ private static CacheableSqmInterpretation buildCacheableSqmInterpretation(
482471
)
483472
.translate();
484473

485-
final FromClauseAccess tableGroupAccess = sqmInterpretation.getFromClauseAccess();
486-
487474
final SqlAstTranslator<JdbcOperationQuerySelect> selectTranslator =
488475
sessionFactory.getJdbcServices().getJdbcEnvironment().getSqlAstTranslatorFactory()
489476
.buildSelectTranslator( sessionFactory, sqmInterpretation.getSqlAst() );
490477

491-
final Map<QueryParameterImplementor<?>, Map<SqmParameter<?>, List<JdbcParametersList>>> jdbcParamsXref
492-
= SqmUtil.generateJdbcParamsXref( domainParameterXref, sqmInterpretation::getJdbcParamsBySqmParam );
478+
final var jdbcParamsXref =
479+
generateJdbcParamsXref( domainParameterXref, sqmInterpretation::getJdbcParamsBySqmParam );
493480

494481
final JdbcParameterBindings jdbcParameterBindings = SqmUtil.createJdbcParameterBindings(
495482
executionContext.getQueryParameterBindings(),
@@ -576,10 +563,7 @@ public void registerLoadingEntityHolder(EntityHolder holder) {
576563

577564
@Override
578565
public String getQueryIdentifier(String sql) {
579-
if ( CRITERIA_HQL_STRING.equals( hql ) ) {
580-
return "[CRITERIA] " + sql;
581-
}
582-
return hql;
566+
return CRITERIA_HQL_STRING.equals( hql ) ? "[CRITERIA] " + sql : hql;
583567
}
584568
}
585569
}

hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public <T, R> T executeQuery(
8989
StatementCreator statementCreator,
9090
ResultsConsumer<T, R> resultsConsumer) {
9191
final PersistenceContext persistenceContext = executionContext.getSession().getPersistenceContext();
92-
boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
93-
Boolean readOnly = executionContext.getQueryOptions().isReadOnly();
92+
final boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
93+
final Boolean readOnly = executionContext.getQueryOptions().isReadOnly();
9494
if ( readOnly != null ) {
9595
// The read-only/modifiable mode for the query was explicitly set.
9696
// Temporarily set the default read-only/modifiable setting to the query's setting.

0 commit comments

Comments
 (0)