1010
1111import org .hibernate .metamodel .MappingMetamodel ;
1212import org .hibernate .metamodel .mapping .BasicValuedModelPart ;
13- import org .hibernate .metamodel .mapping .EntityAssociationMapping ;
1413import org .hibernate .metamodel .mapping .EntityMappingType ;
15- import org .hibernate .metamodel .mapping .MappingType ;
1614import org .hibernate .metamodel .mapping .ModelPart ;
1715import org .hibernate .metamodel .mapping .ModelPartContainer ;
1816import org .hibernate .metamodel .model .domain .EntityDomainType ;
2523import org .hibernate .query .sqm .tree .domain .SqmTreatedPath ;
2624import org .hibernate .spi .NavigablePath ;
2725import org .hibernate .sql .ast .SqlAstWalker ;
26+ import org .hibernate .sql .ast .spi .SqlAstCreationContext ;
2827import org .hibernate .sql .ast .tree .expression .ColumnReference ;
2928import org .hibernate .sql .ast .tree .expression .Expression ;
3029import org .hibernate .sql .ast .tree .expression .SqlSelectionExpression ;
3635
3736import static jakarta .persistence .metamodel .Type .PersistenceType .ENTITY ;
3837import static org .hibernate .internal .util .NullnessUtil .castNonNull ;
38+ import static org .hibernate .query .sqm .internal .SqmUtil .determineAffectedTableName ;
3939import static org .hibernate .query .sqm .internal .SqmUtil .getTargetMappingIfNeeded ;
4040
4141/**
@@ -49,81 +49,87 @@ public static <T> BasicValuedPathInterpretation<T> from(
4949 SqmBasicValuedSimplePath <T > sqmPath ,
5050 SqmToSqlAstConverter sqlAstCreationState ,
5151 boolean jpaQueryComplianceEnabled ) {
52+ final SqlAstCreationContext creationContext = sqlAstCreationState .getCreationContext ();
53+
5254 final SqmPath <?> lhs = sqmPath .getLhs ();
53- final TableGroup tableGroup = sqlAstCreationState .getFromClauseAccess ().getTableGroup ( lhs .getNavigablePath () );
54- EntityMappingType treatTarget = null ;
55+ final TableGroup tableGroup =
56+ sqlAstCreationState .getFromClauseAccess ()
57+ .getTableGroup ( lhs .getNavigablePath () );
58+ final ModelPartContainer tableGroupModelPart = tableGroup .getModelPart ();
59+
60+ final MappingMetamodel mappingMetamodel = creationContext .getMappingMetamodel ();
61+ final EntityMappingType treatTarget ;
5562 final ModelPartContainer modelPartContainer ;
56- if ( lhs instanceof SqmTreatedPath <?, ?> && ( (SqmTreatedPath <?, ?>) lhs ).getTreatTarget ().getPersistenceType () == ENTITY ) {
57- final EntityDomainType <?> treatTargetDomainType = (EntityDomainType <?>) ( (SqmTreatedPath <?, ?>) lhs ).getTreatTarget ();
58-
59- final MappingMetamodel mappingMetamodel = sqlAstCreationState .getCreationContext ().getMappingMetamodel ();
60- final EntityPersister treatEntityDescriptor = mappingMetamodel .findEntityDescriptor ( treatTargetDomainType .getHibernateEntityName () );
61- final MappingType tableGroupMappingType = tableGroup .getModelPart ().getPartMappingType ();
62- if ( tableGroupMappingType instanceof EntityMappingType
63- && treatEntityDescriptor .isTypeOrSuperType ( (EntityMappingType ) tableGroupMappingType ) ) {
64- modelPartContainer = tableGroup .getModelPart ();
63+ if ( lhs instanceof SqmTreatedPath <?, ?> treatedPath
64+ && treatedPath .getTreatTarget ().getPersistenceType () == ENTITY ) {
65+ final EntityDomainType <?> treatTargetDomainType = (EntityDomainType <?>) treatedPath .getTreatTarget ();
66+ final EntityPersister treatEntityDescriptor =
67+ mappingMetamodel .findEntityDescriptor ( treatTargetDomainType .getHibernateEntityName () );
68+ if ( tableGroupModelPart .getPartMappingType () instanceof EntityMappingType entityMappingType
69+ && treatEntityDescriptor .isTypeOrSuperType ( entityMappingType ) ) {
70+ modelPartContainer = tableGroupModelPart ;
6571 treatTarget = treatEntityDescriptor ;
6672 }
6773 else {
6874 modelPartContainer = treatEntityDescriptor ;
75+ treatTarget = null ;
6976 }
7077 }
7178 else {
72- modelPartContainer = tableGroup . getModelPart () ;
73- if ( jpaQueryComplianceEnabled && lhs . getNodeType () instanceof EntityDomainType <?> entityDomainType ) {
74- treatTarget = sqlAstCreationState . getCreationContext (). getMappingMetamodel ()
75- .findEntityDescriptor ( entityDomainType .getHibernateEntityName () );
76- }
79+ modelPartContainer = tableGroupModelPart ;
80+ treatTarget =
81+ jpaQueryComplianceEnabled && lhs . getNodeType () instanceof EntityDomainType <?> entityDomainType
82+ ? mappingMetamodel .findEntityDescriptor ( entityDomainType .getHibernateEntityName () )
83+ : null ;
7784 }
7885
7986 // Use the target type to find the sub part if needed, otherwise just use the container
80- final ModelPart modelPart = getTargetMappingIfNeeded (
81- sqmPath ,
82- modelPartContainer ,
83- sqlAstCreationState
84- ).findSubPart ( sqmPath .getReferencedPathSource ().getPathName (), treatTarget );
85-
87+ final ModelPart modelPart =
88+ getTargetMappingIfNeeded ( sqmPath , modelPartContainer , sqlAstCreationState )
89+ .findSubPart ( sqmPath .getReferencedPathSource ().getPathName (), treatTarget );
8690 if ( modelPart == null ) {
87- if ( jpaQueryComplianceEnabled ) {
88- // to get the better error, see if we got nothing because of treat handling
89- final ModelPart subPart = tableGroup .getModelPart ().findSubPart (
90- sqmPath .getReferencedPathSource ().getPathName (),
91- null
92- );
93- if ( subPart != null ) {
94- throw new StrictJpaComplianceViolation ( StrictJpaComplianceViolation .Type .IMPLICIT_TREAT );
95- }
96- }
97-
98- throw new UnknownPathException ( "Path '" + sqmPath .getNavigablePath () + "' did not reference a known model part" );
91+ modelPartError ( sqmPath , jpaQueryComplianceEnabled , tableGroup );
9992 }
10093
94+ final NavigablePath navigablePath = sqmPath .getNavigablePath ();
10195 final BasicValuedModelPart mapping = castNonNull ( modelPart .asBasicValuedModelPart () );
102- final TableReference tableReference = tableGroup .resolveTableReference (
103- sqmPath .getNavigablePath (),
104- mapping ,
105- mapping .getContainingTableExpression ()
106- );
107-
108- final Expression expression = sqlAstCreationState .getSqlExpressionResolver ().resolveSqlExpression (
109- tableReference ,
110- mapping
111- );
112-
113- final ColumnReference columnReference ;
114- if ( expression instanceof ColumnReference ) {
115- columnReference = ( (ColumnReference ) expression );
96+ final TableReference tableReference =
97+ tableGroup .resolveTableReference ( navigablePath , mapping , mapping .getContainingTableExpression () );
98+ final Expression expression =
99+ sqlAstCreationState .getSqlExpressionResolver ()
100+ .resolveSqlExpression ( tableReference , mapping );
101+ return new BasicValuedPathInterpretation <>( columnReference ( expression ), navigablePath , mapping , tableGroup );
102+ }
103+
104+ private static <T > void modelPartError (
105+ SqmBasicValuedSimplePath <T > sqmPath ,
106+ boolean jpaQueryComplianceEnabled ,
107+ TableGroup tableGroup ) {
108+ if ( jpaQueryComplianceEnabled ) {
109+ // to get the better error, see if we got nothing because of treat handling
110+ final ModelPart subPart =
111+ tableGroup .getModelPart ()
112+ .findSubPart ( sqmPath .getReferencedPathSource ().getPathName (), null );
113+ if ( subPart != null ) {
114+ throw new StrictJpaComplianceViolation ( StrictJpaComplianceViolation .Type .IMPLICIT_TREAT );
115+ }
116+ }
117+
118+ throw new UnknownPathException ( "Path '" + sqmPath .getNavigablePath () + "' did not reference a known model part" );
119+ }
120+
121+ private static ColumnReference columnReference (Expression expression ) {
122+ if ( expression instanceof ColumnReference reference ) {
123+ return reference ;
116124 }
117- else if ( expression instanceof SqlSelectionExpression ) {
118- final Expression selectedExpression = ( ( SqlSelectionExpression ) expression ) .getSelection ().getExpression ();
125+ else if ( expression instanceof SqlSelectionExpression selection ) {
126+ final Expression selectedExpression = selection .getSelection ().getExpression ();
119127 assert selectedExpression instanceof ColumnReference ;
120- columnReference = (ColumnReference ) selectedExpression ;
128+ return (ColumnReference ) selectedExpression ;
121129 }
122130 else {
123131 throw new UnsupportedOperationException ( "Unsupported basic-valued path expression : " + expression );
124132 }
125-
126- return new BasicValuedPathInterpretation <>( columnReference , sqmPath .getNavigablePath (), mapping , tableGroup );
127133 }
128134
129135 private final ColumnReference columnReference ;
@@ -134,18 +140,8 @@ public BasicValuedPathInterpretation(
134140 NavigablePath navigablePath ,
135141 BasicValuedModelPart mapping ,
136142 TableGroup tableGroup ) {
137- this ( columnReference , navigablePath , mapping , tableGroup , determineAffectedTableName ( tableGroup , mapping ) );
138- }
139-
140- private static @ Nullable String determineAffectedTableName (TableGroup tableGroup , BasicValuedModelPart mapping ) {
141- final ModelPartContainer modelPart = tableGroup .getModelPart ();
142- if ( modelPart instanceof EntityAssociationMapping ) {
143- final EntityAssociationMapping associationMapping = (EntityAssociationMapping ) modelPart ;
144- if ( !associationMapping .containsTableReference ( mapping .getContainingTableExpression () ) ) {
145- return associationMapping .getAssociatedEntityMappingType ().getMappedTableDetails ().getTableName ();
146- }
147- }
148- return null ;
143+ this ( columnReference , navigablePath , mapping , tableGroup ,
144+ determineAffectedTableName ( tableGroup , mapping ) );
149145 }
150146
151147 public BasicValuedPathInterpretation (
0 commit comments