HHH-15895 IllegalArgumentException: Cannot create binding for parameter referencen with criteria builder

This commit is contained in:
Andrea Boriero 2023-02-28 20:53:04 +01:00 committed by Steve Ebersole
parent df6ec56978
commit c430834f65
7 changed files with 61 additions and 32 deletions

View File

@ -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<SqmParameter,QueryParameterImplementor<?>> 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<SqmParameter, QueryParameterImplementor<?>> xrefMap = new TreeMap<>(
(o1, o2) ->
o1.compare( o2 )
);
final SqmStatement.ParameterResolutions parameterResolutions = sqmStatement.resolveParameters();

View File

@ -164,4 +164,11 @@ public class JpaCriteriaParameter<T>
}
return Objects.hash( name );
}
@Override
public int compare(SqmParameter anotherParameter) {
return anotherParameter instanceof JpaCriteriaParameter ?
Integer.compare( hashCode(), anotherParameter.hashCode() )
: 1;
}
}

View File

@ -123,6 +123,13 @@ public class SqmJpaCriteriaParameterWrapper<T>
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,

View File

@ -81,4 +81,11 @@ public class SqmNamedParameter<T> extends AbstractSqmParameter<T> {
sb.append( ':' );
sb.append( getName() );
}
@Override
public int compare(SqmParameter anotherParameter) {
return anotherParameter instanceof SqmNamedParameter<?>
? getName().compareTo( ( (SqmNamedParameter<?>) anotherParameter ).getName() )
: -1;
}
}

View File

@ -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<T> extends SqmExpression<T>, JpaParameterExpressio
@Override
SqmParameter<T> 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 );
}
}

View File

@ -85,4 +85,10 @@ public class SqmPositionalParameter<T> extends AbstractSqmParameter<T> {
sb.append( getPosition() );
}
@Override
public int compare(SqmParameter anotherParameter) {
return anotherParameter instanceof SqmPositionalParameter<?>
? getPosition().compareTo( ( (SqmPositionalParameter<?>) anotherParameter ).getPosition() )
: 1;
}
}

View File

@ -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<T> extends JpaCriteriaParameter<T>{
public int hashCode() {
return System.identityHashCode( this );
}
@Override
public int compare(SqmParameter anotherParameter) {
if ( this == anotherParameter ) {
return 0;
}
return 1;
}
}