|
29 | 29 | import org.hibernate.metamodel.mapping.EntityMappingType;
|
30 | 30 | import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
|
31 | 31 | import org.hibernate.metamodel.mapping.JdbcMapping;
|
| 32 | +import org.hibernate.metamodel.mapping.ManagedMappingType; |
32 | 33 | import org.hibernate.metamodel.mapping.MappingModelExpressible;
|
| 34 | +import org.hibernate.metamodel.mapping.ModelPartContainer; |
33 | 35 | import org.hibernate.metamodel.mapping.PluralAttributeMapping;
|
34 | 36 | import org.hibernate.query.IllegalQueryOperationException;
|
35 | 37 | import org.hibernate.query.IllegalSelectQueryException;
|
|
39 | 41 | import org.hibernate.query.sqm.SqmQuerySource;
|
40 | 42 | import org.hibernate.query.sqm.spi.JdbcParameterBySqmParameterAccess;
|
41 | 43 | import org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess;
|
| 44 | +import org.hibernate.query.sqm.sql.SqmToSqlAstConverter; |
42 | 45 | import org.hibernate.query.sqm.tree.SqmDmlStatement;
|
| 46 | +import org.hibernate.query.sqm.tree.SqmJoinType; |
43 | 47 | import org.hibernate.query.sqm.tree.SqmStatement;
|
| 48 | +import org.hibernate.query.sqm.tree.domain.SqmPath; |
44 | 49 | import org.hibernate.query.sqm.tree.expression.JpaCriteriaParameter;
|
45 | 50 | import org.hibernate.query.sqm.tree.expression.SqmJpaCriteriaParameterWrapper;
|
46 | 51 | import org.hibernate.query.sqm.tree.expression.SqmParameter;
|
| 52 | +import org.hibernate.query.sqm.tree.from.SqmFrom; |
| 53 | +import org.hibernate.query.sqm.tree.from.SqmJoin; |
| 54 | +import org.hibernate.query.sqm.tree.select.SqmQueryPart; |
47 | 55 | import org.hibernate.query.sqm.tree.jpa.ParameterCollector;
|
48 | 56 | import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
|
49 | 57 | import org.hibernate.spi.NavigablePath;
|
| 58 | +import org.hibernate.sql.ast.Clause; |
50 | 59 | import org.hibernate.sql.ast.SqlTreeCreationException;
|
51 | 60 | import org.hibernate.sql.ast.tree.expression.JdbcParameter;
|
52 | 61 | import org.hibernate.sql.ast.tree.from.TableGroup;
|
@@ -110,6 +119,31 @@ public static IllegalQueryOperationException expectingNonSelect(SqmStatement<?>
|
110 | 119 | );
|
111 | 120 | }
|
112 | 121 |
|
| 122 | + public static boolean needsTargetTableMapping( |
| 123 | + SqmPath<?> sqmPath, |
| 124 | + ModelPartContainer modelPartContainer, |
| 125 | + SqmToSqlAstConverter sqlAstCreationState) { |
| 126 | + final Clause currentClause = sqlAstCreationState.getCurrentClauseStack().getCurrent(); |
| 127 | + return ( currentClause == Clause.GROUP || currentClause == Clause.SELECT || currentClause == Clause.ORDER || currentClause == Clause.HAVING ) |
| 128 | + && modelPartContainer.getPartMappingType() != modelPartContainer |
| 129 | + && sqmPath.getLhs() instanceof SqmFrom<?, ?> |
| 130 | + && modelPartContainer.getPartMappingType() instanceof ManagedMappingType |
| 131 | + && ( groupByClauseContains( sqlAstCreationState.getCurrentSqmQueryPart(), sqmPath.getNavigablePath() ) |
| 132 | + || isNonOptimizableJoin( sqmPath.getLhs() ) ); |
| 133 | + } |
| 134 | + |
| 135 | + private static boolean groupByClauseContains(SqmQueryPart<?> sqmQueryPart, NavigablePath path) { |
| 136 | + return sqmQueryPart.isSimpleQueryPart() && sqmQueryPart.getFirstQuerySpec().groupByClauseContains( path ); |
| 137 | + } |
| 138 | + |
| 139 | + private static boolean isNonOptimizableJoin(SqmPath<?> sqmPath) { |
| 140 | + if ( sqmPath instanceof SqmJoin<?, ?> ) { |
| 141 | + final SqmJoinType sqmJoinType = ( (SqmJoin<?, ?>) sqmPath ).getSqmJoinType(); |
| 142 | + return sqmJoinType != SqmJoinType.INNER && sqmJoinType != SqmJoinType.LEFT; |
| 143 | + } |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
113 | 147 | public static Map<QueryParameterImplementor<?>, Map<SqmParameter<?>, List<JdbcParametersList>>> generateJdbcParamsXref(
|
114 | 148 | DomainParameterXref domainParameterXref,
|
115 | 149 | JdbcParameterBySqmParameterAccess jdbcParameterBySqmParameterAccess) {
|
|
0 commit comments