HHH-16815 don't copy parameters, since they have identity equality

This commit is contained in:
Gavin King 2023-06-26 14:45:42 +02:00
parent a3abac9065
commit 5d05dd8478
3 changed files with 60 additions and 4 deletions

View File

@ -963,7 +963,7 @@ public class QuerySqmImpl<R>
@Override
public SqmQueryImplementor<R> addOrdering(SingularAttribute<? super R, ?> attribute, SortOrder order) {
if ( sqm instanceof SqmSelectStatement ) {
sqm = sqm.copy( SqmCopyContext.simpleContext() );
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
NodeBuilder nodeBuilder = sqm.nodeBuilder();
SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm;
List<Order> orders = new ArrayList<>( select.getOrderList() );
@ -985,7 +985,7 @@ public class QuerySqmImpl<R>
@Override
public SqmQueryImplementor<R> unordered() {
if ( sqm instanceof SqmSelectStatement ) {
sqm = sqm.copy( SqmCopyContext.simpleContext() );
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm;
select.getQueryPart().setOrderByClause( null );

View File

@ -568,7 +568,7 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
}
private void addOrdering(SingularAttribute<? super R, ?> attribute, SortOrder order) {
sqm = sqm.copy( SqmCopyContext.simpleContext() );
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
NodeBuilder nodeBuilder = sqm.nodeBuilder();
List<Order> orders = new ArrayList<>( sqm.getOrderList() );
sqm.getQuerySpec().getRoots().forEach( root -> {
@ -583,7 +583,7 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
@Override
public SqmSelectionQuery<R> unordered() {
sqm = sqm.copy( SqmCopyContext.simpleContext() );
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
sqm.getQueryPart().setOrderByClause( null );
return this;
}

View File

@ -9,6 +9,7 @@ package org.hibernate.query.sqm.tree;
import java.util.IdentityHashMap;
import org.hibernate.query.sqm.tree.domain.SqmPath;
import org.hibernate.query.sqm.tree.expression.SqmParameter;
/**
*
@ -70,4 +71,59 @@ public interface SqmCopyContext {
}
};
}
static SqmCopyContext noParamCopyContext() {
final IdentityHashMap<Object, Object> map = new IdentityHashMap<>();
return new SqmCopyContext() {
@Override
@SuppressWarnings("unchecked")
public <T> T getCopy(T original) {
if ( original instanceof SqmParameter ) {
return original;
}
if (original instanceof SqmPath) {
return (T) getPathCopy( (SqmPath<?>) original );
}
else {
return (T) map.get( original );
}
}
@Override
public <T> T registerCopy(T original, T copy) {
final Object old = map.put( original, copy );
if ( old != null ) {
throw new IllegalArgumentException( "Already registered a copy: " + old );
}
return copy;
}
@SuppressWarnings("unchecked")
private <T extends SqmPath<?>> T getPathCopy(T original) {
T existing = (T) map.get( original );
if ( existing != null ) {
return existing;
}
SqmPath<?> root = getRoot( original );
if ( root != original ) {
root.copy( this );
// root path might have already copied original
return (T) map.get( original );
}
else {
return null;
}
}
private SqmPath<?> getRoot(SqmPath<?> path) {
if ( path.getLhs() != null ) {
return getRoot( path.getLhs() );
}
else {
return path;
}
}
};
}
}