diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/DomainParameterXref.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/DomainParameterXref.java index 13a3532446..f05090d20f 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/DomainParameterXref.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/DomainParameterXref.java @@ -42,38 +42,9 @@ public class DomainParameterXref { // `.. where a.b = :param or a.c = :param`. Here we have 2 SqmParameter // references (one for each occurrence of `:param`) both of which map to // the same QueryParameter. - final Map> xrefMap = new TreeMap<>( - (o1, o2) -> { - if ( o1 instanceof SqmNamedParameter ) { - final SqmNamedParameter one = (SqmNamedParameter) o1; - return o2 instanceof SqmNamedParameter - ? one.getName().compareTo( ((SqmNamedParameter) o2).getName() ) - : -1; - } - else if ( o1 instanceof SqmPositionalParameter ) { - final SqmPositionalParameter one = (SqmPositionalParameter) o1; - return o2 instanceof SqmPositionalParameter - ? one.getPosition().compareTo( ( (SqmPositionalParameter) o2 ).getPosition() ) - : 1; - } - else if ( o1 instanceof SqmJpaCriteriaParameterWrapper - && o2 instanceof SqmJpaCriteriaParameterWrapper ) { -// final SqmJpaCriteriaParameterWrapper wrapper1 = (SqmJpaCriteriaParameterWrapper) o1; -// final SqmJpaCriteriaParameterWrapper wrapper2 = (SqmJpaCriteriaParameterWrapper) o2; -// if ( wrapper1.getJpaCriteriaParameter() == wrapper2.getJpaCriteriaParameter() ) { -// return 0; -// } -// -// if ( o1.getName() != null ) { -// return o1.getName().compareTo( o2.getName() ); -// } -// else { - return Integer.compare( o1.hashCode(), o2.hashCode() ); -// } - } - - throw new HibernateException( "Unexpected SqmParameter type for comparison : " + o1 + " & " + o2 ); - } + final Map> xrefMap = new TreeMap<>( + (o1, o2) -> + o1.compare( o2 ) ); final SqmStatement.ParameterResolutions parameterResolutions = sqmStatement.resolveParameters(); diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/JpaCriteriaParameter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/JpaCriteriaParameter.java index f7a0ef5e47..25badf0f78 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/JpaCriteriaParameter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/JpaCriteriaParameter.java @@ -164,4 +164,11 @@ public class JpaCriteriaParameter } return Objects.hash( name ); } + + @Override + public int compare(SqmParameter anotherParameter) { + return anotherParameter instanceof JpaCriteriaParameter ? + Integer.compare( hashCode(), anotherParameter.hashCode() ) + : 1; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmJpaCriteriaParameterWrapper.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmJpaCriteriaParameterWrapper.java index b2569b14e3..75403ed589 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmJpaCriteriaParameterWrapper.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmJpaCriteriaParameterWrapper.java @@ -123,6 +123,13 @@ public class SqmJpaCriteriaParameterWrapper jpaCriteriaParameter.appendHqlString( sb ); } + @Override + public int compare(SqmParameter anotherParameter) { + return anotherParameter instanceof SqmJpaCriteriaParameterWrapper ? + getJpaCriteriaParameter().compare( ( (SqmJpaCriteriaParameterWrapper) anotherParameter ).getJpaCriteriaParameter() ) + : 1; + } + // @Override // public Expression toSqlExpression( // Clause clause, diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmNamedParameter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmNamedParameter.java index 5b946119f5..4e4e3a262b 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmNamedParameter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmNamedParameter.java @@ -81,4 +81,11 @@ public class SqmNamedParameter extends AbstractSqmParameter { sb.append( ':' ); sb.append( getName() ); } + + @Override + public int compare(SqmParameter anotherParameter) { + return anotherParameter instanceof SqmNamedParameter + ? getName().compareTo( ( (SqmNamedParameter) anotherParameter ).getName() ) + : -1; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmParameter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmParameter.java index 655c058057..a98977b2c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmParameter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmParameter.java @@ -6,6 +6,7 @@ */ package org.hibernate.query.sqm.tree.expression; +import org.hibernate.HibernateException; import org.hibernate.query.BindableType; import org.hibernate.query.criteria.JpaParameterExpression; import org.hibernate.query.sqm.SqmExpressible; @@ -74,4 +75,24 @@ public interface SqmParameter extends SqmExpression, JpaParameterExpressio @Override SqmParameter copy(SqmCopyContext context); + + default int compare(SqmParameter anotherParameter) { + if ( this instanceof SqmNamedParameter ) { + final SqmNamedParameter one = (SqmNamedParameter) this; + return anotherParameter instanceof SqmNamedParameter + ? one.getName().compareTo( ( (SqmNamedParameter) anotherParameter ).getName() ) + : -1; + } + else if ( this instanceof SqmPositionalParameter ) { + final SqmPositionalParameter one = (SqmPositionalParameter) this; + return anotherParameter instanceof SqmPositionalParameter + ? one.getPosition().compareTo( ( (SqmPositionalParameter) anotherParameter ).getPosition() ) + : 1; + } + else if ( anotherParameter instanceof SqmJpaCriteriaParameterWrapper + && anotherParameter instanceof SqmJpaCriteriaParameterWrapper ) { + return Integer.compare( this.hashCode(), anotherParameter.hashCode() ); + } + throw new HibernateException( "Unexpected SqmParameter type for comparison : " + this + " & " + anotherParameter ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmPositionalParameter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmPositionalParameter.java index 407b6e180c..1a73f1e6f3 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmPositionalParameter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmPositionalParameter.java @@ -85,4 +85,10 @@ public class SqmPositionalParameter extends AbstractSqmParameter { sb.append( getPosition() ); } + @Override + public int compare(SqmParameter anotherParameter) { + return anotherParameter instanceof SqmPositionalParameter + ? getPosition().compareTo( ( (SqmPositionalParameter) anotherParameter ).getPosition() ) + : 1; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/ValueBindJpaCriteriaParameter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/ValueBindJpaCriteriaParameter.java index acf95a01ed..2debc5feb6 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/ValueBindJpaCriteriaParameter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/ValueBindJpaCriteriaParameter.java @@ -6,6 +6,8 @@ */ package org.hibernate.query.sqm.tree.expression; +import java.util.Objects; + import org.hibernate.query.BindableType; import org.hibernate.query.sqm.NodeBuilder; import org.hibernate.query.sqm.tree.SqmCopyContext; @@ -62,4 +64,12 @@ public class ValueBindJpaCriteriaParameter extends JpaCriteriaParameter{ public int hashCode() { return System.identityHashCode( this ); } + + @Override + public int compare(SqmParameter anotherParameter) { + if ( this == anotherParameter ) { + return 0; + } + return 1; + } }