Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@ public interface InternalCache<K, V> {
/**
* Attempt to read from the cache. Will return null on cache miss.
* It would typically be better to use {@link #computeIfAbsent(Object, Function)} instead.
* @param key
* @return
*/
V get(K key);

/**
* Stores a key/value pair into the cache. Storage is not guaranteed, as the implementation
* has liberty to cap internal storage or use various eviction strategies.
* @param key
* @param value
*/
void put(K key, V value);

Expand All @@ -55,7 +51,6 @@ public interface InternalCache<K, V> {
* the general pattern of "try to read, or produce a value and then cache it" but avoiding
* efficiency issues that would be caused by accessing the cache multiple times, not least
* potentially a cache stampede, and concurrent need for generating the same value.
* @param key
* @param mappingFunction This function will be invoked to produce the value, and store it,
* if a matching existing value couldn't be loaded from the cache.
* @return Either the existing value, or the return from the provided function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import org.hibernate.HibernateException;
import org.hibernate.QueryException;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.query.NamedHqlQueryDefinition;
import org.hibernate.boot.query.NamedNativeQueryDefinition;
import org.hibernate.boot.query.NamedProcedureCallDefinition;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.procedure.spi.NamedCallableQueryMemento;
Expand All @@ -33,7 +30,6 @@
import org.hibernate.query.named.NamedQueryMemento;
import org.hibernate.query.named.NamedResultSetMappingMemento;
import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.spi.QueryInterpretationCache;
import org.hibernate.query.sql.spi.NamedNativeQueryMemento;
import org.hibernate.query.sqm.UnknownEntityException;
import org.hibernate.query.sqm.UnknownPathException;
Expand Down Expand Up @@ -70,13 +66,14 @@ public NamedObjectRepositoryImpl(
@Override
@SuppressWarnings("unchecked")
public <R> Map<String, TypedQueryReference<R>> getNamedQueries(Class<R> resultType) {
final Map<String, TypedQueryReference<R>> namedQueries = new HashMap<>( sqmMementoMap.size() + sqlMementoMap.size() );
for ( Map.Entry<String, NamedSqmQueryMemento<?>> entry : sqmMementoMap.entrySet() ) {
final Map<String, TypedQueryReference<R>> namedQueries =
new HashMap<>( sqmMementoMap.size() + sqlMementoMap.size() );
for ( var entry : sqmMementoMap.entrySet() ) {
if ( resultType == entry.getValue().getResultType() ) {
namedQueries.put( entry.getKey(), (TypedQueryReference<R>) entry.getValue() );
}
}
for ( Map.Entry<String, NamedNativeQueryMemento<?>> entry : sqlMementoMap.entrySet() ) {
for ( var entry : sqlMementoMap.entrySet() ) {
if ( resultType == entry.getValue().getResultType() ) {
namedQueries.put( entry.getKey(), (TypedQueryReference<R>) entry.getValue() );
}
Expand Down Expand Up @@ -106,7 +103,6 @@ public void registerNamedQuery(String name, Query query) {
if ( queryImplementor != null ) {
if ( queryImplementor instanceof NativeQueryImplementor<?> nativeQueryImplementor ) {
registerNativeQueryMemento( name, nativeQueryImplementor.toMemento( name ) );

}
else if ( queryImplementor instanceof SqmQueryImplementor<?> sqmQueryImplementor ) {
registerSqmQueryMemento( name, sqmQueryImplementor.toMemento( name ) );
Expand All @@ -127,12 +123,12 @@ else if ( queryImplementor instanceof SqmQueryImplementor<?> sqmQueryImplementor
@Override
public <R> TypedQueryReference<R> registerNamedQuery(String name, TypedQuery<R> query) {
if ( query instanceof NativeQueryImplementor<R> nativeQueryImplementor ) {
final NamedNativeQueryMemento<R> memento = nativeQueryImplementor.toMemento( name );
final var memento = nativeQueryImplementor.toMemento( name );
registerNativeQueryMemento( name, memento );
return memento;
}
else if ( query instanceof SqmQueryImplementor<R> sqmQueryImplementor ) {
final NamedSqmQueryMemento<R> memento = sqmQueryImplementor.toMemento( name );
final var memento = sqmQueryImplementor.toMemento( name );
registerSqmQueryMemento( name, memento );
return memento;
}
Expand Down Expand Up @@ -238,55 +234,51 @@ public NamedQueryMemento<?> resolve(
if ( namedQuery != null ) {
return namedQuery;
}
final NamedHqlQueryDefinition<?> namedHqlQueryDefinition = bootMetamodel.getNamedHqlQueryMapping( registrationName );
final var namedHqlQueryDefinition = bootMetamodel.getNamedHqlQueryMapping( registrationName );
if ( namedHqlQueryDefinition != null ) {
final NamedSqmQueryMemento<?> resolved = namedHqlQueryDefinition.resolve( sessionFactory );
sqmMementoMap.put( namedHqlQueryDefinition.getRegistrationName(), resolved );
return resolved;
final var memento = namedHqlQueryDefinition.resolve( sessionFactory );
sqmMementoMap.put( namedHqlQueryDefinition.getRegistrationName(), memento );
return memento;
}
final NamedNativeQueryDefinition<?> namedNativeQueryDefinition = bootMetamodel.getNamedNativeQueryMapping( registrationName );
final var namedNativeQueryDefinition = bootMetamodel.getNamedNativeQueryMapping( registrationName );
if ( namedNativeQueryDefinition != null ) {
final NamedNativeQueryMemento<?> resolved = namedNativeQueryDefinition.resolve( sessionFactory );
sqlMementoMap.put( namedNativeQueryDefinition.getRegistrationName(), resolved );
return resolved;
final var memento = namedNativeQueryDefinition.resolve( sessionFactory );
sqlMementoMap.put( namedNativeQueryDefinition.getRegistrationName(), memento );
return memento;
}
final NamedProcedureCallDefinition namedCallableQueryDefinition = bootMetamodel.getNamedProcedureCallMapping( registrationName );
final var namedCallableQueryDefinition = bootMetamodel.getNamedProcedureCallMapping( registrationName );
if ( namedCallableQueryDefinition != null ) {
final NamedCallableQueryMemento resolved = namedCallableQueryDefinition.resolve( sessionFactory );
callableMementoMap.put( namedCallableQueryDefinition.getRegistrationName(), resolved );
return resolved;
final var memento = namedCallableQueryDefinition.resolve( sessionFactory );
callableMementoMap.put( namedCallableQueryDefinition.getRegistrationName(), memento );
return memento;
}
return null;
}

@Override
public void prepare(SessionFactoryImplementor sessionFactory, Metadata bootMetamodel) {
bootMetamodel.visitNamedHqlQueryDefinitions(
namedHqlQueryDefinition -> {
final NamedSqmQueryMemento<?> resolved = namedHqlQueryDefinition.resolve( sessionFactory );
sqmMementoMap.put( namedHqlQueryDefinition.getRegistrationName(), resolved );
}
namedHqlQueryDefinition ->
sqmMementoMap.put( namedHqlQueryDefinition.getRegistrationName(),
namedHqlQueryDefinition.resolve( sessionFactory ) )
);

bootMetamodel.visitNamedNativeQueryDefinitions(
namedNativeQueryDefinition -> {
final NamedNativeQueryMemento<?> resolved = namedNativeQueryDefinition.resolve( sessionFactory );
sqlMementoMap.put( namedNativeQueryDefinition.getRegistrationName(), resolved );
}
namedNativeQueryDefinition ->
sqlMementoMap.put( namedNativeQueryDefinition.getRegistrationName(),
namedNativeQueryDefinition.resolve( sessionFactory ) )
);

bootMetamodel.visitNamedResultSetMappingDefinition(
namedResultSetMappingDefinition -> {
final NamedResultSetMappingMemento resolved = namedResultSetMappingDefinition.resolve( () -> sessionFactory );
resultSetMappingMementoMap.put( namedResultSetMappingDefinition.getRegistrationName(), resolved );
}
namedResultSetMappingDefinition ->
resultSetMappingMementoMap.put( namedResultSetMappingDefinition.getRegistrationName(),
namedResultSetMappingDefinition.resolve( () -> sessionFactory ) )
);

bootMetamodel.visitNamedProcedureCallDefinition(
namedProcedureCallDefinition -> {
final NamedCallableQueryMemento resolved = namedProcedureCallDefinition.resolve( sessionFactory );
callableMementoMap.put( namedProcedureCallDefinition.getRegistrationName(), resolved );
}
namedProcedureCallDefinition ->
callableMementoMap.put( namedProcedureCallDefinition.getRegistrationName(),
namedProcedureCallDefinition.resolve( sessionFactory ) )
);

}
Expand All @@ -297,18 +289,17 @@ public void prepare(SessionFactoryImplementor sessionFactory, Metadata bootMetam

@Override
public void validateNamedQueries(QueryEngine queryEngine) {
final Map<String, HibernateException> errors = checkNamedQueries( queryEngine );
final var errors = checkNamedQueries( queryEngine );
if ( !errors.isEmpty() ) {
int i = 0;
final StringBuilder failingQueries = new StringBuilder( "Errors in named queries: " );
for ( Map.Entry<String, HibernateException> entry : errors.entrySet() ) {
for ( var entry : errors.entrySet() ) {
QUERY_MESSAGE_LOGGER.namedQueryError( entry.getKey(), entry.getValue() );
failingQueries.append( "\n" )
.append(" [").append(++i).append("] Error in query named '").append( entry.getKey() ).append("'")
.append(": ").append( entry.getValue().getMessage() );
}
final NamedQueryValidationException exception =
new NamedQueryValidationException( failingQueries.toString(), errors );
final var exception = new NamedQueryValidationException( failingQueries.toString(), errors );
errors.values().forEach( exception::addSuppressed );
throw exception;
}
Expand All @@ -318,20 +309,17 @@ public void validateNamedQueries(QueryEngine queryEngine) {
public Map<String, HibernateException> checkNamedQueries(QueryEngine queryEngine) {
Map<String,HibernateException> errors = new HashMap<>();

final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
final var interpretationCache = queryEngine.getInterpretationCache();
final var hqlTranslator = queryEngine.getHqlTranslator();

// Check named HQL queries
log.tracef( "Checking %s named HQL queries", sqmMementoMap.size() );
for ( NamedSqmQueryMemento<?> hqlMemento : sqmMementoMap.values() ) {
for ( var hqlMemento : sqmMementoMap.values() ) {
final String queryString = hqlMemento.getHqlString();
final String registrationName = hqlMemento.getRegistrationName();
try {
log.tracef( "Checking named HQL query: %s", registrationName );
interpretationCache.resolveHqlInterpretation(
queryString,
null,
queryEngine.getHqlTranslator()
);
interpretationCache.resolveHqlInterpretation( queryString, null, hqlTranslator );
}
catch ( QueryException e ) {
errors.put( registrationName, e );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jakarta.persistence.Parameter;
import org.checkerframework.checker.nullness.qual.Nullable;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
Expand Down Expand Up @@ -65,7 +66,7 @@ public ParameterMetadataImpl(Map<QueryParameterImplementor<?>, List<SqmParameter
Map<Integer, QueryParameterImplementor<?>> tempQueryParametersByPosition = null;
// if we have any ordinal parameters, make sure the numbers
// start with 1 and are contiguous
for ( QueryParameterImplementor<?> queryParameter : queryParameters.keySet() ) {
for ( var queryParameter : queryParameters.keySet() ) {
if ( queryParameter.getPosition() != null ) {
if ( tempQueryParametersByPosition == null ) {
tempQueryParametersByPosition = new HashMap<>();
Expand Down Expand Up @@ -96,24 +97,24 @@ public ParameterMetadataImpl(
Map<String, QueryParameterImplementor<?>> tempQueryParametersByName = null;
Map<Integer, QueryParameterImplementor<?>> tempQueryParametersByPosition = null;
if ( positionalQueryParameters != null ) {
for ( QueryParameterImplementor<?> value : positionalQueryParameters.values() ) {
this.queryParameters.put( value, Collections.emptyList() );
for ( var queryParameter : positionalQueryParameters.values() ) {
this.queryParameters.put( queryParameter, emptyList() );
if ( tempQueryParametersByPosition == null ) {
tempQueryParametersByPosition = new HashMap<>();
}
tempQueryParametersByPosition.put( value.getPosition(), value );
tempQueryParametersByPosition.put( queryParameter.getPosition(), queryParameter );
}
if ( tempQueryParametersByPosition != null ) {
verifyOrdinalParamLabels( tempQueryParametersByPosition.keySet() );
}
}
if ( namedQueryParameters != null ) {
for ( QueryParameterImplementor<?> value : namedQueryParameters.values() ) {
for ( var queryParameter : namedQueryParameters.values() ) {
if ( tempQueryParametersByName == null ) {
tempQueryParametersByName = new HashMap<>();
}
this.queryParameters.put( value, Collections.emptyList() );
tempQueryParametersByName.put( value.getName(), value );
this.queryParameters.put( queryParameter, emptyList() );
tempQueryParametersByName.put( queryParameter.getName(), queryParameter );
}
}
this.queryParametersByPosition = tempQueryParametersByPosition;
Expand Down Expand Up @@ -176,13 +177,12 @@ public int getParameterCount() {

@Override
public <T> BindableType<T> getInferredParameterType(QueryParameter<T> parameter) {
final List<SqmParameter<?>> sqmParameters =
queryParameters.get( (QueryParameterImplementor<T>) parameter );
final var sqmParameters = queryParameters.get( (QueryParameterImplementor<T>) parameter );
if ( sqmParameters == null || sqmParameters.isEmpty() ) {
return null;
}
for ( SqmParameter<?> sqmParameter : sqmParameters ) {
final BindableType<?> nodeType = sqmParameter.getNodeType();
for ( var sqmParameter : sqmParameters ) {
final var nodeType = sqmParameter.getNodeType();
if ( nodeType != null ) {
//noinspection unchecked
return (BindableType<T>) nodeType;
Expand All @@ -209,7 +209,7 @@ public Set<QueryParameterImplementor<?>> getRegistrations() {

@Override
public boolean hasAnyMatching(Predicate<QueryParameterImplementor<?>> filter) {
for ( QueryParameterImplementor<?> queryParameter : queryParameters.keySet() ) {
for ( var queryParameter : queryParameters.keySet() ) {
if ( filter.test( queryParameter ) ) {
return true;
}
Expand Down Expand Up @@ -255,7 +255,7 @@ public QueryParameterImplementor<?> findQueryParameter(String name) {

@Override
public QueryParameterImplementor<?> getQueryParameter(String name) {
final QueryParameterImplementor<?> parameter = findQueryParameter( name );
final var parameter = findQueryParameter( name );
if ( parameter != null ) {
return parameter;
}
Expand Down Expand Up @@ -293,7 +293,7 @@ public QueryParameterImplementor<?> findQueryParameter(int positionLabel) {

@Override
public QueryParameterImplementor<?> getQueryParameter(int positionLabel) {
final QueryParameterImplementor<?> queryParameter = findQueryParameter( positionLabel );
final var queryParameter = findQueryParameter( positionLabel );
if ( queryParameter != null ) {
return queryParameter;
}
Expand Down
Loading