@@ -173,11 +173,9 @@ public static ModelPartContainer getTargetMappingIfNeeded(
173173 if ( sqlAstCreationState .getCurrentClauseStack ().getCurrent () != Clause .FROM
174174 && modelPartContainer .getPartMappingType () != modelPartContainer
175175 && sqmPath .getLhs () instanceof SqmFrom <?, ?> ) {
176- final ModelPart modelPart =
177- modelPartContainer instanceof PluralAttributeMapping pluralAttributeMapping
178- ? getCollectionPart ( pluralAttributeMapping ,
179- castNonNull ( sqmPath .getNavigablePath ().getParent () ) )
180- : modelPartContainer ;
176+ final ModelPart modelPart = modelPartContainer instanceof PluralAttributeMapping plural ?
177+ getCollectionPart ( plural , castNonNull ( sqmPath .getNavigablePath ().getParent () ) )
178+ : modelPartContainer ;
181179 if ( modelPart instanceof EntityAssociationMapping association ) {
182180 if ( shouldRenderTargetSide ( sqmPath , association , sqlAstCreationState ) ) {
183181 return association .getAssociatedEntityMappingType ();
@@ -200,26 +198,18 @@ private static boolean shouldRenderTargetSide(
200198 final Clause clause = sqlAstCreationState .getCurrentClauseStack ().getCurrent ();
201199 return clause == Clause .GROUP || clause == Clause .ORDER
202200 || !isFkOptimizationAllowed ( sqmPath .getLhs (), association )
203- || clauseContainsPath ( Clause .GROUP , sqmPath , sqlAstCreationState )
204- || clauseContainsPath ( Clause .ORDER , sqmPath , sqlAstCreationState );
201+ || inGroupByOrOrderBy ( sqmPath , sqlAstCreationState );
205202 }
206203 }
207204
208- private static boolean clauseContainsPath (
209- Clause clauseToCheck ,
210- SqmPath <?> sqmPath ,
211- SqmToSqlAstConverter sqlAstCreationState ) {
212- final Stack <SqmQueryPart > queryPartStack = sqlAstCreationState .getSqmQueryPartStack ();
213- final NavigablePath navigablePath = sqmPath .getNavigablePath ();
205+ private static boolean inGroupByOrOrderBy (SqmPath <?> sqmPath , SqmToSqlAstConverter converter ) {
206+ final Stack <SqmQueryPart > queryPartStack = converter .getSqmQueryPartStack ();
207+ final NavigablePath np = sqmPath .getNavigablePath ();
214208 final Boolean found = queryPartStack .findCurrentFirst ( queryPart -> {
215209 final SqmQuerySpec <?> spec = queryPart .getFirstQuerySpec ();
216- if ( clauseToCheck == Clause .GROUP && spec .groupByClauseContains ( navigablePath , sqlAstCreationState )
217- || clauseToCheck == Clause .ORDER && spec .orderByClauseContains ( navigablePath , sqlAstCreationState ) ) {
218- return true ;
219- }
220- else {
221- return null ;
222- }
210+ return spec .groupByClauseContains ( np , converter ) || spec .orderByClauseContains ( np , converter ) ?
211+ true :
212+ null ;
223213 } );
224214 return Boolean .TRUE .equals ( found );
225215 }
0 commit comments