From 6f7b9362d2f9e8f8066f6973f4e731ccb059d5fe Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Sat, 31 Dec 2022 12:24:41 +0100 Subject: [PATCH] HHH-15839 CriteriaBuilder treat method on Path causes ClassCastException --- .../query/sqm/sql/BaseSqmToSqlAstConverter.java | 12 ++++++++---- .../query/sqm/tree/domain/SqmTreatedSimplePath.java | 11 +++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index 6e51f56175..f54d6bfd45 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -4797,7 +4797,8 @@ public abstract class BaseSqmToSqlAstConverter extends Base if ( lhs instanceof SqmTreatedPath ) { final SqmTreatedPath treatedPath = (SqmTreatedPath) lhs; 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 ) ) { // Treating a node to a super type can be ignored return expression; @@ -4812,12 +4813,15 @@ public abstract class BaseSqmToSqlAstConverter extends Base final EntityPersister entityDescriptor = domainModel.findEntityDescriptor( treatedPath.getTreatTarget().getHibernateEntityName() ); - conjunctTreatUsages.computeIfAbsent( treatedPath.getWrappedPath(), p -> new HashSet<>( 1 ) ) + conjunctTreatUsages.computeIfAbsent( wrappedPath, p -> new HashSet<>( 1 ) ) .addAll( entityDescriptor.getSubclassEntityNames() ); return expression; } - // Note: If the columns that are accessed are not shared with other entities, we could avoid this wrapping - return createCaseExpression( treatedPath.getWrappedPath(), treatedPath.getTreatTarget(), expression ); + if ( wrappedPath instanceof DiscriminatorSqmPath ) { + // 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; } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmTreatedSimplePath.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmTreatedSimplePath.java index b3c9373517..ce859c826e 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmTreatedSimplePath.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmTreatedSimplePath.java @@ -10,6 +10,7 @@ import org.hibernate.metamodel.model.domain.EntityDomainType; import org.hibernate.query.PathException; import org.hibernate.query.sqm.NodeBuilder; import org.hibernate.query.sqm.SemanticQueryWalker; +import org.hibernate.query.sqm.SqmPathSource; import org.hibernate.query.sqm.tree.SqmCopyContext; /** @@ -31,7 +32,7 @@ public class SqmTreatedSimplePath wrappedPath.getNavigablePath().treatAs( treatTarget.getHibernateEntityName() ), - (EntityDomainType) wrappedPath.getReferencedPathSource(), + (SqmPathSource) wrappedPath.getReferencedPathSource(), wrappedPath.getLhs(), nodeBuilder ); @@ -48,7 +49,7 @@ public class SqmTreatedSimplePath wrappedPath.getNavigablePath().treatAs( treatTarget.getHibernateEntityName() ), - (EntityDomainType) wrappedPath.getReferencedPathSource(), + (SqmPathSource) wrappedPath.getReferencedPathSource(), wrappedPath.getLhs(), nodeBuilder ); @@ -95,6 +96,12 @@ public class SqmTreatedSimplePath return super.treatAs( treatJavaType ); } + @Override + @SuppressWarnings("unchecked") + public SqmPath get(String attributeName) { + return resolvePath( attributeName, treatTarget.getSubPathSource( attributeName ) ); + } + @Override public X accept(SemanticQueryWalker walker) { return walker.visitTreatedPath( this );