HHH-17178 Add `{element} to treated plural joins by default and fix tg registration

This commit is contained in:
Marco Belladelli 2023-09-21 10:45:50 +02:00
parent 7efb077bdd
commit 3875f80611
7 changed files with 26 additions and 45 deletions

View File

@ -2819,29 +2819,10 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
private void registerSqmFromTableGroup(SqmFrom<?, ?> sqmFrom, TableGroup tableGroup) {
getFromClauseIndex().register( sqmFrom, tableGroup );
// We also need to register the table group for the treats
if ( tableGroup instanceof PluralTableGroup ) {
final PluralTableGroup pluralTableGroup = (PluralTableGroup) tableGroup;
for ( SqmFrom<?, ?> sqmTreat : sqmFrom.getSqmTreats() ) {
if ( pluralTableGroup.getElementTableGroup() != null ) {
getFromClauseAccess().registerTableGroup(
sqmTreat.getNavigablePath().append( CollectionPart.Nature.ELEMENT.getName() ),
pluralTableGroup.getElementTableGroup()
);
}
if ( pluralTableGroup.getIndexTableGroup() != null ) {
getFromClauseAccess().registerTableGroup(
sqmTreat.getNavigablePath().append( CollectionPart.Nature.INDEX.getName() ),
pluralTableGroup.getIndexTableGroup()
);
}
}
}
else {
for ( SqmFrom<?, ?> sqmTreat : sqmFrom.getSqmTreats() ) {
getFromClauseAccess().registerTableGroup( sqmTreat.getNavigablePath(), tableGroup );
}
}
}
private TableGroup createCteTableGroup(
String cteName,

View File

@ -21,7 +21,6 @@ import org.hibernate.query.sqm.tree.select.SqmQueryPart;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.spi.FromClauseAccess;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.SqlSelectionExpression;

View File

@ -18,6 +18,7 @@ import org.hibernate.metamodel.mapping.ModelPartContainer;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverter;
import org.hibernate.query.sqm.tree.domain.SqmEmbeddedValuedSimplePath;
import org.hibernate.query.sqm.tree.domain.SqmPath;
import org.hibernate.query.sqm.tree.domain.SqmTreatedPath;
import org.hibernate.query.sqm.tree.from.SqmFrom;
import org.hibernate.query.sqm.tree.select.SqmQueryPart;
@ -43,22 +44,22 @@ public class EmbeddableValuedPathInterpretation<T> extends AbstractSqmPathInterp
SqmEmbeddedValuedSimplePath<T> sqmPath,
SqmToSqlAstConverter sqlAstCreationState,
boolean jpaQueryComplianceEnabled) {
TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().findTableGroup( sqmPath.getLhs().getNavigablePath() );
final SqmPath<?> lhs = sqmPath.getLhs();
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup( lhs.getNavigablePath() );
EntityMappingType treatTarget = null;
if ( jpaQueryComplianceEnabled ) {
final MappingMetamodel mappingMetamodel = sqlAstCreationState.getCreationContext()
.getSessionFactory()
.getRuntimeMetamodels()
.getMappingMetamodel();
if ( sqmPath.getLhs() instanceof SqmTreatedPath ) {
if ( lhs instanceof SqmTreatedPath ) {
//noinspection rawtypes
final EntityDomainType<?> treatTargetDomainType = ( (SqmTreatedPath) sqmPath.getLhs() ).getTreatTarget();
final EntityDomainType<?> treatTargetDomainType = ( (SqmTreatedPath) lhs ).getTreatTarget();
treatTarget = mappingMetamodel.findEntityDescriptor( treatTargetDomainType.getHibernateEntityName() );
}
else if ( sqmPath.getLhs().getNodeType() instanceof EntityDomainType ) {
else if ( lhs.getNodeType() instanceof EntityDomainType ) {
//noinspection rawtypes
final EntityDomainType<?> entityDomainType = (EntityDomainType) sqmPath.getLhs().getNodeType();
final EntityDomainType<?> entityDomainType = (EntityDomainType) lhs.getNodeType();
treatTarget = mappingMetamodel.findEntityDescriptor( entityDomainType.getHibernateEntityName() );
}
@ -72,7 +73,7 @@ public class EmbeddableValuedPathInterpretation<T> extends AbstractSqmPathInterp
final Clause currentClause = sqlAstCreationState.getCurrentClauseStack().getCurrent();
final SqmQueryPart<?> sqmQueryPart = sqlAstCreationState.getCurrentSqmQueryPart();
if ( ( currentClause == Clause.GROUP || currentClause == Clause.SELECT || currentClause == Clause.ORDER || currentClause == Clause.HAVING )
&& sqmPath.getLhs() instanceof SqmFrom<?, ?>
&& lhs instanceof SqmFrom<?, ?>
&& modelPart.getPartMappingType() instanceof ManagedMappingType
&& sqmQueryPart.isSimpleQueryPart()
&& sqmQueryPart.getFirstQuerySpec().groupByClauseContains( sqmPath.getNavigablePath() ) ) {

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.query.sqm.tree.domain;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.model.domain.BagPersistentAttribute;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.query.hql.spi.SqmCreationProcessingState;
@ -28,10 +29,9 @@ public class SqmTreatedBagJoin<O,T, S extends T> extends SqmBagJoin<O,S> impleme
//noinspection unchecked
super(
wrappedPath.getLhs(),
wrappedPath.getNavigablePath().treatAs(
treatTarget.getHibernateEntityName(),
alias
),
wrappedPath.getNavigablePath()
.append( CollectionPart.Nature.ELEMENT.getName() )
.treatAs( treatTarget.getHibernateEntityName(), alias ),
(BagPersistentAttribute<O, S>) wrappedPath.getAttribute(),
alias,
wrappedPath.getSqmJoinType(),

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.query.sqm.tree.domain;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.metamodel.model.domain.ListPersistentAttribute;
import org.hibernate.query.hql.spi.SqmCreationProcessingState;
@ -30,10 +31,9 @@ public class SqmTreatedListJoin<O,T, S extends T> extends SqmListJoin<O,S> imple
//noinspection unchecked
super(
wrappedPath.getLhs(),
wrappedPath.getNavigablePath().treatAs(
treatTarget.getHibernateEntityName(),
alias
),
wrappedPath.getNavigablePath()
.append( CollectionPart.Nature.ELEMENT.getName() )
.treatAs( treatTarget.getHibernateEntityName(), alias ),
(ListPersistentAttribute<O, S>) wrappedPath.getAttribute(),
alias,
wrappedPath.getSqmJoinType(),

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.query.sqm.tree.domain;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.query.hql.spi.SqmCreationProcessingState;
import org.hibernate.query.sqm.SqmPathSource;
@ -26,10 +27,9 @@ public class SqmTreatedMapJoin<O, K, V, S extends V> extends SqmMapJoin<O, K, S>
//noinspection unchecked
super(
wrappedPath.getLhs(),
wrappedPath.getNavigablePath().treatAs(
treatTarget.getHibernateEntityName(),
alias
),
wrappedPath.getNavigablePath()
.append( CollectionPart.Nature.ELEMENT.getName() )
.treatAs( treatTarget.getHibernateEntityName(), alias ),
( (SqmMapJoin<O, K, S>) wrappedPath ).getModel(),
alias,
wrappedPath.getSqmJoinType(),

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.query.sqm.tree.domain;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.metamodel.model.domain.SetPersistentAttribute;
import org.hibernate.query.hql.spi.SqmCreationProcessingState;
@ -28,10 +29,9 @@ public class SqmTreatedSetJoin<O,T, S extends T> extends SqmSetJoin<O,S> impleme
//noinspection unchecked
super(
wrappedPath.getLhs(),
wrappedPath.getNavigablePath().treatAs(
treatTarget.getHibernateEntityName(),
alias
),
wrappedPath.getNavigablePath()
.append( CollectionPart.Nature.ELEMENT.getName() )
.treatAs( treatTarget.getHibernateEntityName(), alias ),
(SetPersistentAttribute<O, S>) wrappedPath.getAttribute(),
alias,
wrappedPath.getSqmJoinType(),