HHH-15839 CriteriaBuilder treat method on Path causes ClassCastException

This commit is contained in:
Andrea Boriero 2022-12-31 12:24:41 +01:00 committed by Andrea Boriero
parent f7ae48ad08
commit 6f7b9362d2
2 changed files with 17 additions and 6 deletions

View File

@ -4797,7 +4797,8 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
if ( lhs instanceof SqmTreatedPath<?, ?> ) { if ( lhs instanceof SqmTreatedPath<?, ?> ) {
final SqmTreatedPath<?, ?> treatedPath = (SqmTreatedPath<?, ?>) lhs; final SqmTreatedPath<?, ?> treatedPath = (SqmTreatedPath<?, ?>) lhs;
final Class<?> treatTargetJavaType = treatedPath.getTreatTarget().getJavaType(); final Class<?> treatTargetJavaType = treatedPath.getTreatTarget().getJavaType();
final Class<?> originalJavaType = treatedPath.getWrappedPath().getJavaType(); final SqmPath<?> wrappedPath = treatedPath.getWrappedPath();
final Class<?> originalJavaType = wrappedPath.getJavaType();
if ( treatTargetJavaType.isAssignableFrom( originalJavaType ) ) { if ( treatTargetJavaType.isAssignableFrom( originalJavaType ) ) {
// Treating a node to a super type can be ignored // Treating a node to a super type can be ignored
return expression; return expression;
@ -4812,12 +4813,15 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
final EntityPersister entityDescriptor = domainModel.findEntityDescriptor( final EntityPersister entityDescriptor = domainModel.findEntityDescriptor(
treatedPath.getTreatTarget().getHibernateEntityName() treatedPath.getTreatTarget().getHibernateEntityName()
); );
conjunctTreatUsages.computeIfAbsent( treatedPath.getWrappedPath(), p -> new HashSet<>( 1 ) ) conjunctTreatUsages.computeIfAbsent( wrappedPath, p -> new HashSet<>( 1 ) )
.addAll( entityDescriptor.getSubclassEntityNames() ); .addAll( entityDescriptor.getSubclassEntityNames() );
return expression; return expression;
} }
// Note: If the columns that are accessed are not shared with other entities, we could avoid this wrapping if ( wrappedPath instanceof DiscriminatorSqmPath ) {
return createCaseExpression( treatedPath.getWrappedPath(), treatedPath.getTreatTarget(), expression ); // Note: If the columns that are accessed are not shared with other entities, we could avoid this wrapping
return createCaseExpression( wrappedPath, treatedPath.getTreatTarget(), expression );
}
return expression;
} }
return expression; return expression;
} }

View File

@ -10,6 +10,7 @@ import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.query.PathException; import org.hibernate.query.PathException;
import org.hibernate.query.sqm.NodeBuilder; import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SemanticQueryWalker; import org.hibernate.query.sqm.SemanticQueryWalker;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.query.sqm.tree.SqmCopyContext; import org.hibernate.query.sqm.tree.SqmCopyContext;
/** /**
@ -31,7 +32,7 @@ public class SqmTreatedSimplePath<T, S extends T>
wrappedPath.getNavigablePath().treatAs( wrappedPath.getNavigablePath().treatAs(
treatTarget.getHibernateEntityName() treatTarget.getHibernateEntityName()
), ),
(EntityDomainType<S>) wrappedPath.getReferencedPathSource(), (SqmPathSource<S>) wrappedPath.getReferencedPathSource(),
wrappedPath.getLhs(), wrappedPath.getLhs(),
nodeBuilder nodeBuilder
); );
@ -48,7 +49,7 @@ public class SqmTreatedSimplePath<T, S extends T>
wrappedPath.getNavigablePath().treatAs( wrappedPath.getNavigablePath().treatAs(
treatTarget.getHibernateEntityName() treatTarget.getHibernateEntityName()
), ),
(EntityDomainType<S>) wrappedPath.getReferencedPathSource(), (SqmPathSource<S>) wrappedPath.getReferencedPathSource(),
wrappedPath.getLhs(), wrappedPath.getLhs(),
nodeBuilder nodeBuilder
); );
@ -95,6 +96,12 @@ public class SqmTreatedSimplePath<T, S extends T>
return super.treatAs( treatJavaType ); return super.treatAs( treatJavaType );
} }
@Override
@SuppressWarnings("unchecked")
public SqmPath<?> get(String attributeName) {
return resolvePath( attributeName, treatTarget.getSubPathSource( attributeName ) );
}
@Override @Override
public <X> X accept(SemanticQueryWalker<X> walker) { public <X> X accept(SemanticQueryWalker<X> walker) {
return walker.visitTreatedPath( this ); return walker.visitTreatedPath( this );