make HqlInterpretation generic to eliminate warnings / unchecked casts

and delete a deprecated method of an @Incubating API
This commit is contained in:
Gavin King 2024-02-19 11:47:10 +01:00
parent e4632107d7
commit 27bf0b8523
9 changed files with 51 additions and 81 deletions

View File

@ -767,7 +767,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
delayedAfterCompletion();
try {
final HqlInterpretation interpretation = interpretHql( hql, resultType );
final HqlInterpretation<R> interpretation = interpretHql( hql, resultType );
checkSelectionQuery( hql, interpretation );
return createSelectionQuery( hql, resultType, interpretation );
}
@ -777,7 +777,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
}
}
private <R> SelectionQuery<R> createSelectionQuery(String hql, Class<R> resultType, HqlInterpretation interpretation) {
private <R> SelectionQuery<R> createSelectionQuery(String hql, Class<R> resultType, HqlInterpretation<R> interpretation) {
final SqmSelectionQueryImpl<R> query = new SqmSelectionQueryImpl<>( hql, interpretation, resultType, this );
if ( resultType != null ) {
checkResultType( resultType, query );
@ -787,7 +787,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return query;
}
protected <R> HqlInterpretation interpretHql(String hql, Class<R> resultType) {
protected <R> HqlInterpretation<R> interpretHql(String hql, Class<R> resultType) {
final QueryEngine queryEngine = getFactory().getQueryEngine();
return queryEngine.getInterpretationCache()
.resolveHqlInterpretation(
@ -797,7 +797,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
);
}
protected static void checkSelectionQuery(String hql, HqlInterpretation hqlInterpretation) {
protected static void checkSelectionQuery(String hql, HqlInterpretation<?> hqlInterpretation) {
if ( !( hqlInterpretation.getSqmStatement() instanceof SqmSelectStatement ) ) {
throw new IllegalSelectQueryException( "Expecting a selection query, but found `" + hql + "`", hql);
}
@ -840,7 +840,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
delayedAfterCompletion();
try {
final HqlInterpretation interpretation = interpretHql( queryString, expectedResultType );
final HqlInterpretation<T> interpretation = interpretHql( queryString, expectedResultType );
final QuerySqmImpl<T> query = new QuerySqmImpl<>( queryString, interpretation, expectedResultType, this );
applyQuerySettingsAndHints( query );
query.setComment( queryString );

View File

@ -249,7 +249,7 @@ public class NamedObjectRepositoryImpl implements NamedObjectRepository {
interpretationCache.resolveHqlInterpretation(
queryString,
null,
s -> queryEngine.getHqlTranslator().translate( queryString, null )
queryEngine.getHqlTranslator()
);
}
catch ( QueryException e ) {

View File

@ -10,6 +10,7 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.spi.HqlInterpretation;
import org.hibernate.query.spi.NonSelectQueryPlan;
import org.hibernate.query.spi.ParameterMetadataImplementor;
@ -60,15 +61,16 @@ public class QueryInterpretationCacheDisabledImpl implements QueryInterpretation
}
@Override
public HqlInterpretation resolveHqlInterpretation(String queryString, Class<?> expectedResultType, Function<String, SqmStatement<?>> creator) {
public <R> HqlInterpretation<R> resolveHqlInterpretation(
String queryString, Class<R> expectedResultType, HqlTranslator translator) {
final StatisticsImplementor statistics = statisticsSupplier.get();
final boolean stats = statistics.isStatisticsEnabled();
final long startTime = stats ? System.nanoTime() : 0L;
final SqmStatement<?> sqmStatement = creator.apply( queryString );
final SqmStatement<R> sqmStatement = translator.translate( queryString, expectedResultType );
final DomainParameterXref domainParameterXref;
final ParameterMetadataImplementor parameterMetadata;
if ( sqmStatement.getSqmParameters().isEmpty() ) {
domainParameterXref = DomainParameterXref.empty();
parameterMetadata = ParameterMetadataImpl.EMPTY;
@ -84,9 +86,9 @@ public class QueryInterpretationCacheDisabledImpl implements QueryInterpretation
statistics.queryCompiled( queryString, microseconds );
}
return new HqlInterpretation() {
return new HqlInterpretation<>() {
@Override
public SqmStatement<?> getSqmStatement() {
public SqmStatement<R> getSqmStatement() {
return sqmStatement;
}

View File

@ -40,7 +40,7 @@ public class QueryInterpretationCacheStandardImpl implements QueryInterpretation
*/
private final BoundedConcurrentHashMap<Key, QueryPlan> queryPlanCache;
private final BoundedConcurrentHashMap<Object, HqlInterpretation> hqlInterpretationCache;
private final BoundedConcurrentHashMap<Object, HqlInterpretation<?>> hqlInterpretationCache;
private final BoundedConcurrentHashMap<String, ParameterInterpretation> nativeQueryParamCache;
private final Supplier<StatisticsImplementor> statisticsSupplier;
@ -100,69 +100,49 @@ public class QueryInterpretationCacheStandardImpl implements QueryInterpretation
}
@Override
public HqlInterpretation resolveHqlInterpretation(
public <R> HqlInterpretation<R> resolveHqlInterpretation(
String queryString,
Class<?> expectedResultType,
Function<String, SqmStatement<?>> creator) {
return resolveHqlInterpretation( queryString, expectedResultType, new HqlTranslator() {
@Override
public <R> SqmStatement<R> translate(String hql, Class<R> expectedResultType) {
//noinspection unchecked
return (SqmStatement<R>) creator.apply( hql );
}
} );
}
@Override
public HqlInterpretation resolveHqlInterpretation(
String queryString,
Class<?> expectedResultType,
Class<R> expectedResultType,
HqlTranslator translator) {
log.tracef( "QueryPlan#resolveHqlInterpretation( `%s` )", queryString );
final StatisticsImplementor statistics = statisticsSupplier.get();
final Object cacheKey;
if ( expectedResultType != null ) {
cacheKey = new HqlInterpretationCacheKey( queryString, expectedResultType );
}
else {
cacheKey = queryString;
}
final HqlInterpretation existing = hqlInterpretationCache.get( cacheKey );
final Object cacheKey = expectedResultType != null
? new HqlInterpretationCacheKey( queryString, expectedResultType )
: queryString;
final HqlInterpretation<?> existing = hqlInterpretationCache.get( cacheKey );
if ( existing != null ) {
if ( statistics.isStatisticsEnabled() ) {
statistics.queryPlanCacheHit( queryString );
}
return existing;
return (HqlInterpretation<R>) existing;
}
else if ( expectedResultType != null ) {
final HqlInterpretation existingQueryOnly = hqlInterpretationCache.get( queryString );
final HqlInterpretation<?> existingQueryOnly = hqlInterpretationCache.get( queryString );
if ( existingQueryOnly != null ) {
if ( statistics.isStatisticsEnabled() ) {
statistics.queryPlanCacheHit( queryString );
}
return existingQueryOnly;
return (HqlInterpretation<R>) existingQueryOnly;
}
}
final HqlInterpretation hqlInterpretation = createHqlInterpretation(
queryString,
expectedResultType,
translator,
statistics
);
final HqlInterpretation<R> hqlInterpretation =
createHqlInterpretation( queryString, expectedResultType, translator, statistics );
hqlInterpretationCache.put( cacheKey, hqlInterpretation );
return hqlInterpretation;
}
protected static HqlInterpretation createHqlInterpretation(
protected static <R> HqlInterpretation<R> createHqlInterpretation(
String queryString,
Class<?> expectedResultType,
Class<R> expectedResultType,
HqlTranslator translator,
StatisticsImplementor statistics) {
final boolean stats = statistics.isStatisticsEnabled();
final long startTime = stats ? System.nanoTime() : 0L;
final SqmStatement<?> sqmStatement = translator.translate( queryString, expectedResultType );
final SqmStatement<R> sqmStatement = translator.translate( queryString, expectedResultType );
final ParameterMetadataImplementor parameterMetadata;
final DomainParameterXref domainParameterXref;
@ -181,7 +161,7 @@ public class QueryInterpretationCacheStandardImpl implements QueryInterpretation
statistics.queryCompiled( queryString, microseconds );
}
return new SimpleHqlInterpretationImpl( sqmStatement, parameterMetadata, domainParameterXref );
return new SimpleHqlInterpretationImpl<>( sqmStatement, parameterMetadata, domainParameterXref );
}
@Override

View File

@ -11,9 +11,11 @@ import org.hibernate.query.sqm.tree.SqmStatement;
/**
* @author Steve Ebersole
*
* @param <R> the query result type
*/
public interface HqlInterpretation {
SqmStatement<?> getSqmStatement();
public interface HqlInterpretation<R> {
SqmStatement<R> getSqmStatement();
ParameterMetadataImplementor getParameterMetadata();

View File

@ -37,12 +37,7 @@ public interface QueryInterpretationCache {
int getNumberOfCachedHqlInterpretations();
int getNumberOfCachedQueryPlans();
@Deprecated(forRemoval = true)
HqlInterpretation resolveHqlInterpretation(String queryString, Class<?> expectedResultType, Function<String, SqmStatement<?>> creator);
default HqlInterpretation resolveHqlInterpretation(String queryString, Class<?> expectedResultType, HqlTranslator translator) {
return resolveHqlInterpretation( queryString, expectedResultType, s -> translator.translate( queryString, expectedResultType ) );
}
<R> HqlInterpretation<R> resolveHqlInterpretation(String queryString, Class<R> expectedResultType, HqlTranslator translator);
<R> SelectQueryPlan<R> resolveSelectQueryPlan(Key key, Supplier<SelectQueryPlan<R>> creator);

View File

@ -12,13 +12,13 @@ import org.hibernate.query.sqm.tree.SqmStatement;
/**
* @author Steve Ebersole
*/
public class SimpleHqlInterpretationImpl implements HqlInterpretation {
private final SqmStatement<?> sqmStatement;
public class SimpleHqlInterpretationImpl<R> implements HqlInterpretation<R> {
private final SqmStatement<R> sqmStatement;
private final ParameterMetadataImplementor parameterMetadata;
private final DomainParameterXref domainParameterXref;
public SimpleHqlInterpretationImpl(
SqmStatement<?> sqmStatement,
SqmStatement<R> sqmStatement,
ParameterMetadataImplementor parameterMetadata,
DomainParameterXref domainParameterXref) {
this.sqmStatement = sqmStatement;
@ -27,7 +27,7 @@ public class SimpleHqlInterpretationImpl implements HqlInterpretation {
}
@Override
public SqmStatement<?> getSqmStatement() {
public SqmStatement<R> getSqmStatement() {
return sqmStatement;
}

View File

@ -167,13 +167,10 @@ public class QuerySqmImpl<R>
final QueryEngine queryEngine = session.getFactory().getQueryEngine();
final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
final HqlInterpretation hqlInterpretation = interpretationCache.resolveHqlInterpretation(
hql,
expectedResultType,
(s) -> queryEngine.getHqlTranslator().translate( hql, expectedResultType )
);
final HqlInterpretation<R> hqlInterpretation =
interpretationCache.resolveHqlInterpretation( hql, expectedResultType, queryEngine.getHqlTranslator() );
this.sqm = (SqmStatement<R>) hqlInterpretation.getSqmStatement();
this.sqm = hqlInterpretation.getSqmStatement();
this.parameterMetadata = hqlInterpretation.getParameterMetadata();
this.domainParameterXref = hqlInterpretation.getDomainParameterXref();
@ -200,17 +197,16 @@ public class QuerySqmImpl<R>
/**
* Form used for HQL queries
*/
@SuppressWarnings("unchecked")
public QuerySqmImpl(
String hql,
HqlInterpretation hqlInterpretation,
HqlInterpretation<R> hqlInterpretation,
Class<R> resultType,
SharedSessionContractImplementor session) {
super( session );
this.hql = hql;
this.resultType = resultType;
this.sqm = (SqmStatement<R>) hqlInterpretation.getSqmStatement();
this.sqm = hqlInterpretation.getSqmStatement();
this.parameterMetadata = hqlInterpretation.getParameterMetadata();
this.domainParameterXref = hqlInterpretation.getDomainParameterXref();
@ -897,7 +893,7 @@ public class QuerySqmImpl<R>
}
@Override
public SqmQueryImplementor<R> setResultListTransformer(ResultListTransformer transformer) {
public SqmQueryImplementor<R> setResultListTransformer(ResultListTransformer<R> transformer) {
applyResultListTransformer( transformer );
return this;
}
@ -1248,7 +1244,7 @@ public class QuerySqmImpl<R>
}
@Override
public SqmQueryImplementor<R> setProperties(Map bean) {
public SqmQueryImplementor<R> setProperties(@SuppressWarnings("rawtypes") Map bean) {
super.setProperties( bean );
return this;
}
@ -1362,7 +1358,7 @@ public class QuerySqmImpl<R>
}
@Override
public SqmQueryImplementor<R> setParameterList(String name, Collection values) {
public SqmQueryImplementor<R> setParameterList(String name, @SuppressWarnings("rawtypes") Collection values) {
super.setParameterList( name, values );
return this;
}

View File

@ -101,13 +101,12 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
public SqmSelectionQueryImpl(
String hql,
HqlInterpretation hqlInterpretation,
HqlInterpretation<R> hqlInterpretation,
Class<R> expectedResultType,
SharedSessionContractImplementor session) {
super( session );
this.hql = hql;
//noinspection unchecked
this.sqm = (SqmSelectStatement<R>) hqlInterpretation.getSqmStatement();
this.parameterMetadata = hqlInterpretation.getParameterMetadata();
@ -166,14 +165,10 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
final QueryEngine queryEngine = session.getFactory().getQueryEngine();
final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
final HqlInterpretation hqlInterpretation = interpretationCache.resolveHqlInterpretation(
hql,
resultType,
(s) -> queryEngine.getHqlTranslator().translate( hql, resultType )
);
final HqlInterpretation<R> hqlInterpretation =
interpretationCache.resolveHqlInterpretation( hql, resultType, queryEngine.getHqlTranslator() );
SqmUtil.verifyIsSelectStatement( hqlInterpretation.getSqmStatement(), hql );
//noinspection unchecked
this.sqm = (SqmSelectStatement<R>) hqlInterpretation.getSqmStatement();
this.parameterMetadata = hqlInterpretation.getParameterMetadata();