HHH-16483 Delay copying of cte statement definitions to handle recursion

This commit is contained in:
Marco Belladelli 2023-05-02 15:18:39 +02:00
parent 444df0cdfe
commit 45ec259efe
No known key found for this signature in database
GPG Key ID: D1D0C3030AE3AA35
1 changed files with 7 additions and 3 deletions

View File

@ -42,7 +42,7 @@ import jakarta.persistence.criteria.Subquery;
public class SqmCteStatement<T> extends AbstractSqmNode implements SqmVisitableNode, JpaCteCriteria<T> { public class SqmCteStatement<T> extends AbstractSqmNode implements SqmVisitableNode, JpaCteCriteria<T> {
private final SqmCteContainer cteContainer; private final SqmCteContainer cteContainer;
private final SqmCteTable<T> cteTable; private final SqmCteTable<T> cteTable;
private final SqmSelectQuery<?> cteDefinition; private SqmSelectQuery<?> cteDefinition;
private CteMaterialization materialization; private CteMaterialization materialization;
private CteSearchClauseKind searchClauseKind; private CteSearchClauseKind searchClauseKind;
private List<JpaSearchOrder> searchBySpecifications; private List<JpaSearchOrder> searchBySpecifications;
@ -146,13 +146,13 @@ public class SqmCteStatement<T> extends AbstractSqmNode implements SqmVisitableN
if ( existing != null ) { if ( existing != null ) {
return existing; return existing;
} }
return context.registerCopy( final SqmCteStatement<T> copy = context.registerCopy(
this, this,
new SqmCteStatement<>( new SqmCteStatement<>(
nodeBuilder(), nodeBuilder(),
cteContainer, cteContainer,
cteTable, cteTable,
cteDefinition.copy( context ), null,
materialization, materialization,
searchClauseKind, searchClauseKind,
searchBySpecifications, searchBySpecifications,
@ -164,6 +164,10 @@ public class SqmCteStatement<T> extends AbstractSqmNode implements SqmVisitableN
noCycleValue == null ? null : noCycleValue.copy( context ) noCycleValue == null ? null : noCycleValue.copy( context )
) )
); );
// We have to copy the definition object after registering the copy of this because for recursive CTEs
// the select query from clause may contain the current cte statement itself
copy.cteDefinition = cteDefinition.copy( context );
return copy;
} }
@Override @Override