HHH-13769: Avoid unnecessary joins

This commit is contained in:
Steve Ebersole 2019-12-09 17:30:21 -06:00
parent 79c83cf8ba
commit 3be2369110
56 changed files with 735 additions and 254 deletions

View File

@ -83,7 +83,7 @@ class DatabaseSnapshotExecutor {
final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(
rootPath,
null,
null,
true,
LockMode.NONE,
sqlAliasBaseManager,
state.getSqlExpressionResolver(),

View File

@ -192,7 +192,7 @@ public class LoaderSelectBuilder {
final TableGroup rootTableGroup = loadable.createRootTableGroup(
rootNavigablePath,
null,
null,
true,
lockOptions.getLockMode(),
sqlAstCreationState.getSqlAliasBaseManager(),
sqlAstCreationState.getSqlExpressionResolver(),
@ -449,7 +449,7 @@ public class LoaderSelectBuilder {
final TableGroup rootTableGroup = loadable.createRootTableGroup(
rootNavigablePath,
null,
null,
true,
lockOptions.getLockMode(),
sqlAstCreationState.getSqlAliasBaseManager(),
sqlAstCreationState.getSqlExpressionResolver(),

View File

@ -15,7 +15,6 @@ import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.tree.from.RootTableGroupProducer;
@ -42,7 +41,7 @@ public interface Loadable extends ModelPart, RootTableGroupProducer {
default TableGroup createRootTableGroup(
NavigablePath navigablePath,
String explicitSourceAlias,
JoinType tableReferenceJoinType,
boolean canUseInnerJoins,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,

View File

@ -6,6 +6,11 @@
*/
package org.hibernate.metamodel.mapping;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.from.TableGroupBuilder;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.results.graph.Fetchable;
/**
@ -62,4 +67,6 @@ public interface CollectionPart extends ModelPart, Fetchable {
default String getPartName() {
return getNature().getName();
}
void applyPrimaryTableReference(TableGroupBuilder tableGroupBuilder, SqlExpressionResolver sqlExpressionResolver, SqlAstCreationContext creationContext);
}

View File

@ -18,7 +18,7 @@ import org.hibernate.loader.ast.spi.Loadable;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
@ -222,7 +222,7 @@ public interface EntityMappingType extends ManagedMappingType, Loadable {
default TableGroup createRootTableGroup(
NavigablePath navigablePath,
String explicitSourceAlias,
JoinType tableReferenceJoinType,
boolean canUseInnerJoins,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -231,7 +231,7 @@ public interface EntityMappingType extends ManagedMappingType, Loadable {
return getEntityPersister().createRootTableGroup(
navigablePath,
explicitSourceAlias,
tableReferenceJoinType,
canUseInnerJoins,
lockMode,
aliasBaseGenerator,
sqlExpressionResolver,
@ -243,13 +243,13 @@ public interface EntityMappingType extends ManagedMappingType, Loadable {
@Override
default void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
getEntityPersister().applyTableReferences(
sqlAliasBase,
baseJoinType,
baseSqlAstJoinType,
collector,
sqlExpressionResolver,
creationContext

View File

@ -7,7 +7,7 @@
package org.hibernate.metamodel.mapping;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.from.TableGroup;
@ -27,14 +27,14 @@ public interface ForeignKeyDescriptor extends VirtualModelPart {
Predicate generateJoinPredicate(
TableGroup lhs,
TableGroup tableGroup,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext);
Predicate generateJoinPredicate(
TableReference lhs,
TableReference rhs,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext);

View File

@ -19,10 +19,14 @@ import org.hibernate.metamodel.mapping.MappingType;
import org.hibernate.metamodel.model.convert.spi.BasicValueConverter;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupBuilder;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.results.ResultsLogger;
import org.hibernate.sql.results.graph.basic.BasicFetch;
import org.hibernate.sql.results.graph.basic.BasicResult;
@ -74,6 +78,14 @@ public class BasicValuedCollectionPart implements CollectionPart, BasicValuedMod
return mapper;
}
@Override
public void applyPrimaryTableReference(
TableGroupBuilder tableGroupBuilder,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
// nothing to do
}
@Override
public String getContainingTableExpression() {
return tableExpression;

View File

@ -23,6 +23,7 @@ import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupBuilder;
import org.hibernate.sql.results.graph.basic.BasicFetch;
import org.hibernate.sql.results.graph.basic.BasicResult;
import org.hibernate.sql.results.graph.DomainResult;
@ -77,6 +78,14 @@ public class CollectionIdentifierDescriptorImpl implements CollectionIdentifierD
return type;
}
@Override
public void applyPrimaryTableReference(
TableGroupBuilder tableGroupBuilder,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
// nothing to do
}
@Override
public JdbcMapping getJdbcMapping() {
return type;

View File

@ -16,6 +16,7 @@ import org.hibernate.engine.FetchStrategy;
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch;
@ -36,7 +37,7 @@ import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.query.NavigablePath;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverter;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
@ -234,7 +235,7 @@ public class EmbeddedAttributeMapping
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -245,11 +246,11 @@ public class EmbeddedAttributeMapping
lhs
);
lhs.addTableGroupJoin( new TableGroupJoin( navigablePath, JoinType.INNER, compositeTableGroup, null ) );
lhs.addTableGroupJoin( new TableGroupJoin( navigablePath, SqlAstJoinType.INNER, compositeTableGroup, null ) );
return new TableGroupJoin(
navigablePath,
joinType,
sqlAstJoinType,
compositeTableGroup
);
}
@ -257,7 +258,7 @@ public class EmbeddedAttributeMapping
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
@ -266,7 +267,7 @@ public class EmbeddedAttributeMapping
if ( attrMapping instanceof TableGroupProducer ) {
( (TableGroupProducer) attrMapping ).applyTableReferences(
sqlAliasBase,
baseJoinType,
baseSqlAstJoinType,
collector,
sqlExpressionResolver,
creationContext
@ -275,7 +276,7 @@ public class EmbeddedAttributeMapping
else if ( attrMapping.getMappedTypeDescriptor() instanceof TableGroupProducer ) {
( (TableGroupProducer) attrMapping.getMappedTypeDescriptor() ).applyTableReferences(
sqlAliasBase,
baseJoinType,
baseSqlAstJoinType,
collector,
sqlExpressionResolver,
creationContext
@ -285,6 +286,25 @@ public class EmbeddedAttributeMapping
);
}
@Override
public TableReference createPrimaryTableReference(
SqlAliasBase sqlAliasBase,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
throw new UnsupportedOperationException( "Unexpected call to EmbeddedAttributeMapping#createPrimaryTableReference" );
}
@Override
public TableReferenceJoin createTableReferenceJoin(
String joinTableExpression,
SqlAliasBase sqlAliasBase,
TableReference lhs,
boolean canUseInnerJoin,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
throw new UnsupportedOperationException( "Unexpected call to EmbeddedAttributeMapping#createTableReferenceJoin" );
}
@Override
public String getSqlAliasStem() {
return getAttributeName();

View File

@ -23,7 +23,7 @@ import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverter;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
@ -34,8 +34,11 @@ import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.from.CompositeTableGroup;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupBuilder;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceCollector;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.FetchParent;
@ -168,7 +171,7 @@ public class EmbeddedCollectionPart implements CollectionPart, EmbeddableValuedF
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -179,7 +182,7 @@ public class EmbeddedCollectionPart implements CollectionPart, EmbeddableValuedF
return new TableGroupJoin(
navigablePath,
joinType,
sqlAstJoinType,
tableGroup,
null
);
@ -188,13 +191,39 @@ public class EmbeddedCollectionPart implements CollectionPart, EmbeddableValuedF
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
// nothing to do
}
@Override
public void applyPrimaryTableReference(
TableGroupBuilder tableGroupBuilder,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
}
@Override
public TableReference createPrimaryTableReference(
SqlAliasBase sqlAliasBase,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
throw new UnsupportedOperationException( "Unexpected call to EmbeddedCollectionPart#createPrimaryTableReference" );
}
@Override
public TableReferenceJoin createTableReferenceJoin(
String joinTableExpression,
SqlAliasBase sqlAliasBase,
TableReference lhs,
boolean canUseInnerJoin,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
throw new UnsupportedOperationException( "Unexpected call to EmbeddedCollectionPart#createTableReferenceJoin" );
}
@Override
public String getSqlAliasStem() {
return sqlAliasStem;

View File

@ -15,6 +15,7 @@ import org.hibernate.engine.FetchStrategy;
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.results.graph.embeddable.EmbeddableValuedFetchable;
import org.hibernate.metamodel.mapping.ColumnConsumer;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
@ -29,7 +30,7 @@ import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.query.NavigablePath;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverter;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
@ -195,7 +196,7 @@ public class EmbeddedIdentifierMappingImpl
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -206,11 +207,11 @@ public class EmbeddedIdentifierMappingImpl
lhs
);
lhs.addTableGroupJoin( new TableGroupJoin( navigablePath, JoinType.INNER, compositeTableGroup, null ) );
lhs.addTableGroupJoin( new TableGroupJoin( navigablePath, SqlAstJoinType.INNER, compositeTableGroup, null ) );
return new TableGroupJoin(
navigablePath,
joinType,
sqlAstJoinType,
compositeTableGroup
);
}
@ -234,7 +235,7 @@ public class EmbeddedIdentifierMappingImpl
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
@ -243,7 +244,7 @@ public class EmbeddedIdentifierMappingImpl
if ( attrMapping instanceof TableGroupProducer ) {
( (TableGroupProducer) attrMapping ).applyTableReferences(
sqlAliasBase,
baseJoinType,
baseSqlAstJoinType,
collector,
sqlExpressionResolver,
creationContext
@ -252,7 +253,7 @@ public class EmbeddedIdentifierMappingImpl
else if ( attrMapping.getMappedTypeDescriptor() instanceof TableGroupProducer ) {
( (TableGroupProducer) attrMapping.getMappedTypeDescriptor() ).applyTableReferences(
sqlAliasBase,
baseJoinType,
baseSqlAstJoinType,
collector,
sqlExpressionResolver,
creationContext
@ -262,6 +263,25 @@ public class EmbeddedIdentifierMappingImpl
);
}
@Override
public TableReference createPrimaryTableReference(
SqlAliasBase sqlAliasBase,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
throw new UnsupportedOperationException( "Unexpected call to EmbeddedIdentifierImpl#createPrimaryTableReference" );
}
@Override
public TableReferenceJoin createTableReferenceJoin(
String joinTableExpression,
SqlAliasBase sqlAliasBase,
TableReference lhs,
boolean canUseInnerJoin,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
throw new UnsupportedOperationException( "Unexpected call to EmbeddedIdentifierImpl#createTableReferenceJoin" );
}
@Override
public String getFetchableName() {
return name;

View File

@ -18,7 +18,14 @@ import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseManager;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupBuilder;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.FetchParent;
@ -71,6 +78,24 @@ public class EntityCollectionPart implements CollectionPart, EntityAssociationMa
return getEntityMappingType();
}
@Override
public void applyPrimaryTableReference(
TableGroupBuilder tableGroupBuilder,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
tableGroupBuilder.applySecondaryTableReferences(
getEntityMappingType().createPrimaryTableReference( tableGroupBuilder.getSqlAliasBase(), sqlExpressionResolver, creationContext ),
SqlAstJoinType.LEFT,
(lhs, rhs, sqlAstJoinType) -> foreignKeyDescriptor.generateJoinPredicate(
lhs,
rhs,
sqlAstJoinType,
sqlExpressionResolver,
creationContext
)
);
}
@Override
public EntityMappingType getEntityMappingType() {
return entityMappingType;
@ -113,13 +138,19 @@ public class EntityCollectionPart implements CollectionPart, EntityAssociationMa
assert fetchParent.getReferencedMappingContainer() instanceof PluralAttributeMapping;
// find or create the TableGroup associated with this `fetchablePath`
final TableGroup tableGroup = creationState.getSqlAstCreationState().getFromClauseAccess().resolveTableGroup(
creationState.getSqlAstCreationState().getFromClauseAccess().resolveTableGroup(
fetchablePath,
np -> {
// we need to create one. first, find the collection's TableGroup
// We need to create one. The Result will be able to find it later by path
// first, find the collection's TableGroup
final TableGroup collectionTableGroup = creationState.getSqlAstCreationState()
.getFromClauseAccess()
.getTableGroup( fetchablePath.getParent() );
.getTableGroup( fetchParent.getNavigablePath() );
assert collectionTableGroup != null;
// create a "wrapper" around the collection TableGroup adding in the entity's table references
return new EntityCollectionPartTableGroup( fetchablePath, collectionTableGroup, this );
}
);

View File

@ -29,7 +29,7 @@ import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.Joinable;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAliasStemHelper;
@ -39,16 +39,17 @@ import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupBuilder;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceCollector;
import org.hibernate.sql.ast.tree.from.TableReferenceContributor;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult;
import org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch;
import org.hibernate.sql.results.graph.collection.internal.EagerCollectionFetch;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.FetchParent;
import org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult;
import org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch;
import org.hibernate.sql.results.graph.collection.internal.EagerCollectionFetch;
/**
* @author Steve Ebersole
@ -252,7 +253,7 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping impleme
fetchablePath,
lhsTableGroup,
null,
JoinType.LEFT,
SqlAstJoinType.LEFT,
lockMode,
creationState.getSqlAliasBaseManager(),
creationState.getSqlAstCreationState().getSqlExpressionResolver(),
@ -295,7 +296,7 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping impleme
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -308,12 +309,27 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping impleme
this,
lockMode,
sqlAliasBase,
(tableExpression, tableGroup) -> createTableReferenceJoin(
tableExpression,
sqlAliasBase,
tableGroup.getPrimaryTableReference(),
sqlAstJoinType == SqlAstJoinType.INNER && ! getAttributeMetadataAccess().resolveAttributeMetadata( null ).isNullable(),
sqlExpressionResolver,
creationContext
),
creationContext.getSessionFactory()
);
applyTableReferences(
sqlAliasBase,
joinType,
tableGroupBuilder.applyPrimaryReference(
// returns null when there is no "collection table"
getCollectionDescriptor().createPrimaryTableReference(
sqlAliasBase,
sqlExpressionResolver,
creationContext
)
);
elementDescriptor.applyPrimaryTableReference(
tableGroupBuilder,
sqlExpressionResolver,
creationContext
@ -322,12 +338,12 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping impleme
final TableGroup tableGroup = tableGroupBuilder.build();
final TableGroupJoin tableGroupJoin = new TableGroupJoin(
navigablePath,
joinType,
sqlAstJoinType,
tableGroup,
getKeyDescriptor().generateJoinPredicate(
lhs,
tableGroup,
joinType,
sqlAstJoinType,
sqlExpressionResolver,
creationContext
)
@ -338,21 +354,57 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping impleme
return tableGroupJoin;
}
@Override
public TableReferenceJoin createTableReferenceJoin(
String joinTableExpression,
SqlAliasBase sqlAliasBase,
TableReference lhs,
boolean sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
return getCollectionDescriptor().createTableReferenceJoin(
joinTableExpression,
sqlAliasBase,
lhs,
sqlAstJoinType,
sqlExpressionResolver,
creationContext
);
}
@Override
public TableReference createPrimaryTableReference(
SqlAliasBase sqlAliasBase,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
return getCollectionDescriptor().createPrimaryTableReference(
sqlAliasBase,
sqlExpressionResolver,
creationContext
);
}
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
getCollectionDescriptor().applyTableReferences( sqlAliasBase, baseJoinType, collector, sqlExpressionResolver, creationContext );
getCollectionDescriptor().applyTableReferences(
sqlAliasBase,
baseSqlAstJoinType,
collector,
sqlExpressionResolver,
creationContext
);
}
@Override
public TableGroup createRootTableGroup(
NavigablePath navigablePath,
String explicitSourceAlias,
JoinType tableReferenceJoinType,
boolean canUseInnerJoins,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -365,17 +417,54 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping impleme
this,
lockMode,
sqlAliasBase,
(tableExpression, tableGroup) -> createTableReferenceJoin(
tableExpression,
sqlAliasBase,
tableGroup.getPrimaryTableReference(),
canUseInnerJoins && ! getAttributeMetadataAccess().resolveAttributeMetadata( null ).isNullable(),
sqlExpressionResolver,
creationContext
),
creationContext.getSessionFactory()
);
applyTableReferences(
sqlAliasBase,
tableReferenceJoinType,
getElementDescriptor().applyPrimaryTableReference(
tableGroupBuilder,
sqlExpressionResolver,
creationContext
);
if ( getIndexDescriptor() != null ) {
getIndexDescriptor().applyPrimaryTableReference(
tableGroupBuilder,
sqlExpressionResolver,
creationContext
);
}
final TableReference collectionTableReference = getCollectionDescriptor().createPrimaryTableReference(
sqlAliasBase,
sqlExpressionResolver,
creationContext
);
if ( collectionTableReference != null ) {
tableGroupBuilder.applySecondaryTableReferences(
collectionTableReference,
canUseInnerJoins && ! getAttributeMetadataAccess().resolveAttributeMetadata( null ).isNullable()
? SqlAstJoinType.INNER
: SqlAstJoinType.LEFT,
(lhs, rhs, sqlAstJoinType) -> fkDescriptor.generateJoinPredicate(
lhs,
rhs,
sqlAstJoinType,
sqlExpressionResolver,
creationContext
)
);
}
return tableGroupBuilder.build();
}

View File

@ -23,7 +23,7 @@ import org.hibernate.metamodel.model.convert.spi.BasicValueConverter;
import org.hibernate.query.ComparisonOperator;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
@ -109,7 +109,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
public Predicate generateJoinPredicate(
TableReference lhs,
TableReference rhs,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
if ( lhs.getTableExpression().equals( keyColumnContainingTable ) ) {
@ -154,7 +154,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
public Predicate generateJoinPredicate(
TableGroup lhs,
TableGroup tableGroup,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
final TableReference keyTableReference = getTableReference( lhs, tableGroup, keyColumnContainingTable );
@ -244,10 +244,9 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
return tableGroup.getPrimaryTableReference();
}
for ( TableReferenceJoin tableJoin : lhs.getTableReferenceJoins() ) {
if ( tableJoin.getJoinedTableReference().getTableExpression().equals( table ) ) {
return tableJoin.getJoinedTableReference();
}
final TableReference tableReference = lhs.resolveTableReference( table );
if ( tableReference != null ) {
return tableReference;
}
throw new IllegalStateException( "Could not resolve binding for table `" + table + "`" );

View File

@ -20,7 +20,7 @@ import org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAliasStemHelper;
@ -32,7 +32,9 @@ import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupBuilder;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableGroupJoinProducer;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceCollector;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
@ -120,18 +122,18 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
if ( fetchTiming == FetchTiming.IMMEDIATE && selected ) {
if ( sqlAstCreationState.getFromClauseAccess().findTableGroup( fetchablePath ) == null ) {
JoinType joinType;
SqlAstJoinType sqlAstJoinType;
if ( isNullable ) {
joinType = JoinType.LEFT;
sqlAstJoinType = SqlAstJoinType.LEFT;
}
else {
joinType = JoinType.INNER;
sqlAstJoinType = SqlAstJoinType.INNER;
}
final TableGroupJoin tableGroupJoin = createTableGroupJoin(
fetchablePath,
lhsTableGroup,
null,
joinType,
sqlAstJoinType,
lockMode,
creationState.getSqlAliasBaseManager(),
creationState.getSqlAstCreationState().getSqlExpressionResolver(),
@ -199,7 +201,7 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -212,23 +214,39 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
this,
lockMode,
sqlAliasBase,
(tableExpression, tableGroup) -> createTableReferenceJoin(
tableExpression,
sqlAliasBase,
tableGroup.getPrimaryTableReference(),
false,
sqlExpressionResolver,
creationContext
),
creationContext.getSessionFactory()
);
applyTableReferences(
sqlAliasBase,
joinType,
tableGroupBuilder,
sqlExpressionResolver,
creationContext
tableGroupBuilder.applyPrimaryReference(
getEntityMappingType().createPrimaryTableReference(
sqlAliasBase,
sqlExpressionResolver,
creationContext
)
);
// applyTableReferences(
// sqlAliasBase,
// joinType,
// tableGroupBuilder,
// sqlExpressionResolver,
// creationContext
// );
getMappedTypeDescriptor().getIdentifierMapping();
final TableGroup tableGroup = tableGroupBuilder.build();
final TableGroupJoin tableGroupJoin = new TableGroupJoin(
navigablePath,
joinType,
sqlAstJoinType,
tableGroup,
null
);
@ -238,7 +256,7 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
final Predicate predicate = foreignKeyDescriptor.generateJoinPredicate(
lhs,
tableGroup,
joinType,
sqlAstJoinType,
sqlExpressionResolver,
creationContext
);
@ -252,16 +270,42 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
return sqlAliasStem;
}
@Override
public TableReference createPrimaryTableReference(
SqlAliasBase sqlAliasBase,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
return getEntityMappingType().createPrimaryTableReference( sqlAliasBase, sqlExpressionResolver, creationContext );
}
@Override
public TableReferenceJoin createTableReferenceJoin(
String joinTableExpression,
SqlAliasBase sqlAliasBase,
TableReference lhs,
boolean canUseInnerJoin,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
return getEntityMappingType().createTableReferenceJoin(
joinTableExpression,
sqlAliasBase,
lhs,
canUseInnerJoin && ! getAttributeMetadataAccess().resolveAttributeMetadata( null ).isNullable(),
sqlExpressionResolver,
creationContext
);
}
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
getMappedTypeDescriptor().applyTableReferences(
sqlAliasBase,
baseJoinType,
baseSqlAstJoinType,
collector,
sqlExpressionResolver,
creationContext

View File

@ -109,7 +109,7 @@ import org.hibernate.sql.Alias;
import org.hibernate.sql.SelectFragment;
import org.hibernate.sql.SimpleSelect;
import org.hibernate.sql.Template;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
@ -2468,10 +2468,69 @@ public abstract class AbstractCollectionPersister
return false;
}
@Override
public TableReference createPrimaryTableReference(
SqlAliasBase sqlAliasBase,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
if ( qualifiedTableName != null && ! isOneToMany() ) {
return new TableReference(
qualifiedTableName,
sqlAliasBase.generateNewAlias(),
false,
getFactory()
);
}
return null;
}
@Override
public TableReferenceJoin createTableReferenceJoin(
String joinTableExpression,
SqlAliasBase sqlAliasBase,
TableReference lhs,
boolean canUseInnerJoin,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
if ( elementPersister == null ) {
return null;
}
final String entityPrimaryTableName = ( (Joinable) elementPersister ).getTableName();
if ( entityPrimaryTableName.equals( joinTableExpression ) ) {
assert qualifiedTableName != null;
assert lhs.getTableExpression().equals( qualifiedTableName );
final TableReference tableReference = elementPersister.createPrimaryTableReference( sqlAliasBase, sqlExpressionResolver, creationContext );
final SqlAstJoinType sqlAstJoinType = canUseInnerJoin ? SqlAstJoinType.INNER : SqlAstJoinType.LEFT;
return new TableReferenceJoin(
sqlAstJoinType,
tableReference,
generateEntityElementJoinPredicate(
lhs,
tableReference,
sqlAstJoinType,
sqlExpressionResolver,
creationContext
)
);
}
return elementPersister.createTableReferenceJoin(
joinTableExpression,
sqlAliasBase,
lhs,
canUseInnerJoin,
sqlExpressionResolver,
creationContext
);
}
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
@ -2499,10 +2558,10 @@ public abstract class AbstractCollectionPersister
// entity's primary table
collector.applyPrimaryJoinProducer(
(lhs, rhs) -> new TableReferenceJoin(
baseJoinType,
baseSqlAstJoinType,
rhs,
generateEntityElementJoinPredicate(
lhs, rhs, baseJoinType, sqlExpressionResolver, creationContext
lhs, rhs, baseSqlAstJoinType, sqlExpressionResolver, creationContext
)
)
);
@ -2510,7 +2569,7 @@ public abstract class AbstractCollectionPersister
elementPersister.applyTableReferences(
sqlAliasBase,
// todo (6.0) : determine the proper join-type to use
JoinType.LEFT,
SqlAstJoinType.LEFT,
collector,
sqlExpressionResolver,
creationContext
@ -2523,12 +2582,12 @@ public abstract class AbstractCollectionPersister
collector.applyPrimaryJoinProducer(
(lhs, rhs) -> new TableReferenceJoin(
baseJoinType,
baseSqlAstJoinType,
rhs,
generateIndexJoinPredicate(
lhs,
rhs,
JoinType.CROSS,
SqlAstJoinType.CROSS,
sqlExpressionResolver,
creationContext
)
@ -2537,7 +2596,7 @@ public abstract class AbstractCollectionPersister
contributor.applyTableReferences(
sqlAliasBase,
baseJoinType,
baseSqlAstJoinType,
collector,
sqlExpressionResolver,
creationContext
@ -2548,7 +2607,7 @@ public abstract class AbstractCollectionPersister
private Predicate generateIndexJoinPredicate(
TableReference lhs,
TableReference rhs,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
final CollectionPart indexDescriptor = attributeMapping.getIndexDescriptor();
@ -2649,7 +2708,7 @@ public abstract class AbstractCollectionPersister
private Predicate generateEntityElementJoinPredicate(
TableReference lhs,
TableReference rhs,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();

View File

@ -11,7 +11,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Comparator;
import java.util.Map;
import java.util.function.Consumer;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
@ -26,22 +25,18 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.metadata.CollectionMetadata;
import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.model.convert.spi.BasicValueConverter;
import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.walking.spi.CollectionDefinition;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasStemHelper;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.from.TableReferenceCollector;
import org.hibernate.sql.ast.tree.from.TableReferenceContributor;
import org.hibernate.type.CollectionType;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/**
* A strategy for persisting a collection role. Defines a contract between
@ -384,7 +379,7 @@ public interface CollectionPersister extends CollectionDefinition, TableReferenc
@Override
default void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {

View File

@ -568,5 +568,4 @@ public class OneToManyPersister extends AbstractCollectionPersister {
public FilterAliasGenerator getFilterAliasGenerator(String rootAlias) {
return getElementPersister().getFilterAliasGenerator( rootAlias );
}
}

View File

@ -173,6 +173,7 @@ import org.hibernate.sql.SimpleSelect;
import org.hibernate.sql.Template;
import org.hibernate.sql.Update;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAliasStemHelper;
@ -1216,7 +1217,7 @@ public abstract class AbstractEntityPersister
public TableGroup createRootTableGroup(
NavigablePath navigablePath,
String explicitSourceAlias,
org.hibernate.sql.ast.JoinType tableReferenceJoinType,
boolean canUseInnerJoins,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -1229,24 +1230,104 @@ public abstract class AbstractEntityPersister
this,
lockMode,
sqlAliasBase,
(tableExpression, tableGroup) -> {
for ( int i = 0; i < getSubclassTableSpan(); i++ ) {
final String subclassTableName = getSubclassTableName( i );
if ( subclassTableName.equals( tableExpression ) ) {
final boolean isNullableTable = isNullableSubclassTable( i );
final TableReference joinedTableReference = new TableReference(
tableExpression,
sqlAliasBase.generateNewAlias(),
isNullableTable,
getFactory()
);
return new TableReferenceJoin(
determineSubclassTableJoinType(
i,
canUseInnerJoins,
true,
Collections.emptySet()
),
joinedTableReference,
generateJoinPredicate(
tableGroup.getPrimaryTableReference(),
joinedTableReference,
i,
sqlExpressionResolver
)
);
}
}
return null;
},
creationContext.getSessionFactory()
);
applyTableReferences(
sqlAliasBase,
tableReferenceJoinType,
builder,
sqlExpressionResolver,
creationContext
builder.applyPrimaryReference(
createPrimaryTableReference(
sqlAliasBase,
sqlExpressionResolver,
creationContext
)
);
// applyTableReferences(
// sqlAliasBase,
// tableReferenceJoinType,
// builder,
// sqlExpressionResolver,
// creationContext
// );
return builder.build();
}
protected TableReference resolvePrimaryTableReference(
@Override
public TableReference createPrimaryTableReference(
SqlAliasBase sqlAliasBase,
SqlExpressionResolver sqlExpressionResolver) {
// todo (6.0) : temporary
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
return resolvePrimaryTableReference( sqlAliasBase );
}
@Override
public TableReferenceJoin createTableReferenceJoin(
String joinTableExpression,
SqlAliasBase sqlAliasBase,
TableReference lhs,
boolean canUseInnerJoin,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
for ( int i = 1; i < getSubclassTableSpan(); i++ ) {
final String subclassTableName = getSubclassTableName( i );
if ( subclassTableName.equals( joinTableExpression ) ) {
final TableReference joinedTableReference = new TableReference(
joinTableExpression,
sqlAliasBase.generateNewAlias(),
isNullableSubclassTable( i ),
getFactory()
);
return new TableReferenceJoin(
canUseInnerJoin ? SqlAstJoinType.INNER : SqlAstJoinType.LEFT,
joinedTableReference,
generateJoinPredicate(
lhs,
joinedTableReference,
i,
sqlExpressionResolver
)
);
}
}
return null;
}
protected TableReference resolvePrimaryTableReference(SqlAliasBase sqlAliasBase) {
return new TableReference(
getRootTableName(),
sqlAliasBase.generateNewAlias(),
@ -1258,14 +1339,11 @@ public abstract class AbstractEntityPersister
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
org.hibernate.sql.ast.JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
final TableReference primaryTableReference = resolvePrimaryTableReference(
sqlAliasBase,
sqlExpressionResolver
);
final TableReference primaryTableReference = resolvePrimaryTableReference( sqlAliasBase );
collector.applyPrimaryReference( primaryTableReference );
@ -1285,7 +1363,7 @@ public abstract class AbstractEntityPersister
protected TableReferenceJoin createTableReferenceJoin(
int subClassTablePosition,
TableReference rootTableReference,
org.hibernate.sql.ast.JoinType joinType,
SqlAstJoinType sqlAstJoinType,
SqlAliasBase sqlAliasBase,
SqlExpressionResolver sqlExpressionResolver) {
final boolean nullable = isNullableSubclassTable( subClassTablePosition );
@ -1298,7 +1376,7 @@ public abstract class AbstractEntityPersister
);
return new TableReferenceJoin(
nullable ? org.hibernate.sql.ast.JoinType.LEFT : joinType,
nullable ? SqlAstJoinType.LEFT : sqlAstJoinType,
joinedTableReference,
generateJoinPredicate( rootTableReference, joinedTableReference, subClassTablePosition, sqlExpressionResolver )
);
@ -4091,7 +4169,7 @@ public abstract class AbstractEntityPersister
return join;
}
protected org.hibernate.sql.ast.JoinType determineSubclassTableJoinType(
protected SqlAstJoinType determineSubclassTableJoinType(
int subclassTableNumber,
boolean canInnerJoin,
boolean includeSubclasses,
@ -4102,7 +4180,7 @@ public abstract class AbstractEntityPersister
&& !isNullableTable( subclassTableNumber );
// the table is either this persister's driving table or (one of) its super class persister's driving
// tables which can be inner joined as long as the `shouldInnerJoin` condition resolves to true
return shouldInnerJoin ? org.hibernate.sql.ast.JoinType.INNER : org.hibernate.sql.ast.JoinType.LEFT;
return shouldInnerJoin ? SqlAstJoinType.INNER : SqlAstJoinType.LEFT;
}
// otherwise we have a subclass table and need to look a little deeper...
@ -4112,15 +4190,15 @@ public abstract class AbstractEntityPersister
// so we give TREAT-AS higher precedence...
if ( isSubclassTableIndicatedByTreatAsDeclarations( subclassTableNumber, treatAsDeclarations ) ) {
return org.hibernate.sql.ast.JoinType.INNER;
return SqlAstJoinType.INNER;
}
if ( includeSubclasses
&& !isSubclassTableSequentialSelect( subclassTableNumber )
&& !isSubclassTableLazy( subclassTableNumber ) ) {
return org.hibernate.sql.ast.JoinType.LEFT;
return SqlAstJoinType.LEFT;
}
return org.hibernate.sql.ast.JoinType.INNER;
return SqlAstJoinType.INNER;
}
protected JoinType determineSubclassTableJoinType(

View File

@ -12,9 +12,11 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Supplier;
@ -59,7 +61,6 @@ import org.hibernate.sql.CaseFragment;
import org.hibernate.sql.InFragment;
import org.hibernate.sql.Insert;
import org.hibernate.sql.SelectFragment;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
@ -531,7 +532,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
if ( persistentClass.isPolymorphic() ) {
subclassesByDiscriminatorValue.put( discriminatorValue, getEntityName() );
discriminatorValuesByTableName = new HashMap<>( subclassSpan + 1 );
discriminatorValuesByTableName = new LinkedHashMap<>( subclassSpan + 1 );
subclassNameByTableName = new HashMap<>( subclassSpan + 1);
discriminatorValuesByTableName.put( persistentClass.getTable().getName(), discriminatorSQLString);
discriminatorValues = new String[subclassSpan];
@ -1216,7 +1217,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
public TableGroup createRootTableGroup(
NavigablePath navigablePath,
String explicitSourceAlias,
JoinType tableReferenceJoinType,
boolean canUseInnerJoins,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -1225,7 +1226,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
return super.createRootTableGroup(
navigablePath,
explicitSourceAlias,
tableReferenceJoinType,
canUseInnerJoins,
lockMode,
aliasBaseGenerator,
sqlExpressionResolver,
@ -1279,25 +1280,30 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
CaseSearchedExpressionInfo info = new CaseSearchedExpressionInfo();
final TableReference primaryTableReference = entityTableGroup.getPrimaryTableReference();
final List<TableReferenceJoin> tableReferenceJoins = entityTableGroup.getTableReferenceJoins();
final BasicType discriminatorType = (BasicType) getDiscriminatorType();
final CaseSearchedExpression caseSearchedExpression = new CaseSearchedExpression( discriminatorType );
for ( int i = tableReferenceJoins.size() - 1; i >= 0; i-- ) {
final TableReferenceJoin tableReferenceJoin = tableReferenceJoins.get( i );
final TableReference joinedTableReference = tableReferenceJoin.getJoinedTableReference();
if ( discriminatorValuesByTableName.containsKey( joinedTableReference.getTableExpression() ) ) {
final ColumnReference identifierColumnReference = getIdentifierColumnReference(
joinedTableReference );
info.columnReferences.add( identifierColumnReference );
addWhen(
caseSearchedExpression,
joinedTableReference,
identifierColumnReference,
discriminatorType
);
}
}
discriminatorValuesByTableName.forEach(
(tableName, discriminatorValue) -> {
if ( ! primaryTableReference.getTableExpression().equals( tableName ) ) {
TableReference tableReference = entityTableGroup.getTableReference( tableName );
if ( tableReference == null ) {
// we have not yet created a TableReference for this sub-class table, but we need to because
// it has a discriminator value associated with it
tableReference = entityTableGroup.resolveTableReference( tableName );
}
final ColumnReference identifierColumnReference = getIdentifierColumnReference( tableReference );
info.columnReferences.add( identifierColumnReference );
addWhen(
caseSearchedExpression,
tableReference,
identifierColumnReference,
discriminatorType
);
}
}
);
addWhen(
caseSearchedExpression,

View File

@ -46,7 +46,6 @@ import org.hibernate.query.NavigablePath;
import org.hibernate.sql.InFragment;
import org.hibernate.sql.Insert;
import org.hibernate.sql.SelectFragment;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
@ -882,7 +881,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
public TableGroup createRootTableGroup(
NavigablePath navigablePath,
String explicitSourceAlias,
JoinType tableReferenceJoinType,
boolean canUseInnerJoins,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
@ -891,7 +890,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
final TableGroup tableGroup = super.createRootTableGroup(
navigablePath,
explicitSourceAlias,
tableReferenceJoinType,
canUseInnerJoins,
lockMode,
aliasBaseGenerator,
sqlExpressionResolver,

View File

@ -47,8 +47,6 @@ import org.hibernate.persister.spi.PersisterCreationContext;
import org.hibernate.property.access.spi.Setter;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.SelectFragment;
import org.hibernate.sql.SimpleSelect;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
@ -233,7 +231,7 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
public TableGroup createRootTableGroup(
NavigablePath navigablePath,
String explicitSourceAlias,
JoinType tableReferenceJoinType,
boolean canUseInnerJoins,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,

View File

@ -27,19 +27,18 @@ import org.hibernate.query.sqm.tree.predicate.SqmWhereClause;
import org.hibernate.query.sqm.tree.select.SqmSelectClause;
import org.hibernate.query.sqm.tree.update.SqmAssignment;
import org.hibernate.query.sqm.tree.update.SqmSetClause;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlAstProcessingState;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.ast.tree.select.QuerySpec;
import org.hibernate.sql.ast.tree.update.Assignable;
import org.hibernate.sql.ast.tree.update.Assignment;
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.results.graph.DomainResultCreationState;
/**
@ -80,7 +79,7 @@ public class MultiTableSqmMutationConverter extends BaseSqmToSqlAstConverter imp
this.mutatingTableGroup = mutatingEntityDescriptor.createRootTableGroup(
navigablePath,
null,
JoinType.LEFT,
true,
LockMode.PESSIMISTIC_WRITE,
getSqlAliasBaseGenerator(),
getSqlExpressionResolver(),
@ -89,9 +88,6 @@ public class MultiTableSqmMutationConverter extends BaseSqmToSqlAstConverter imp
creationContext.getSessionFactory()
);
// because this is a multi-table update, here we expect multiple TableReferences
assert !mutatingTableGroup.getTableReferenceJoins().isEmpty();
getFromClauseAccess().registerTableGroup( navigablePath, mutatingTableGroup );
}

View File

@ -166,6 +166,7 @@ public final class ExecuteWithIdTableHelper {
idTableReference,
Collections.emptyList(),
null,
null,
executionContext.getSession().getFactory()
);

View File

@ -47,6 +47,7 @@ public final class ExecuteWithoutIdTableHelper {
rootTableReference,
Collections.emptyList(),
null,
null,
sessionFactory
);

View File

@ -99,7 +99,7 @@ import org.hibernate.query.sqm.tree.select.SqmSortSpecification;
import org.hibernate.query.sqm.tree.select.SqmSubQuery;
import org.hibernate.query.sqm.tree.update.SqmUpdateStatement;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.FromClauseAccess;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAliasBaseManager;
@ -487,7 +487,7 @@ public abstract class BaseSqmToSqlAstConverter
final TableGroup tableGroup = entityDescriptor.createRootTableGroup(
sqmRoot.getNavigablePath(),
sqmRoot.getExplicitAlias(),
JoinType.INNER,
true,
LockMode.NONE,
sqlAliasBaseManager,
getSqlExpressionResolver(),
@ -573,7 +573,7 @@ public abstract class BaseSqmToSqlAstConverter
final TableGroup tableGroup = entityDescriptor.createRootTableGroup(
sqmJoin.getNavigablePath(),
sqmJoin.getExplicitAlias(),
JoinType.CROSS,
true,
determineLockMode( sqmJoin.getExplicitAlias() ),
sqlAliasBaseManager,
getSqlExpressionResolver(),
@ -583,7 +583,7 @@ public abstract class BaseSqmToSqlAstConverter
final TableGroupJoin tableGroupJoin = new TableGroupJoin(
sqmJoin.getNavigablePath(),
JoinType.CROSS,
SqlAstJoinType.CROSS,
tableGroup
);
@ -601,7 +601,7 @@ public abstract class BaseSqmToSqlAstConverter
final TableGroup tableGroup = entityDescriptor.createRootTableGroup(
sqmJoin.getNavigablePath(),
sqmJoin.getExplicitAlias(),
sqmJoin.getSqmJoinType().getCorrespondingSqlJoinType(),
true,
determineLockMode( sqmJoin.getExplicitAlias() ),
sqlAliasBaseManager,
getSqlExpressionResolver(),
@ -651,7 +651,7 @@ public abstract class BaseSqmToSqlAstConverter
joinedPath.getNavigablePath(),
tableGroup,
null,
tableGroup.isInnerJoinPossible() ? JoinType.INNER : JoinType.LEFT,
tableGroup.isInnerJoinPossible() ? SqlAstJoinType.INNER : SqlAstJoinType.LEFT,
null,
sqlAliasBaseManager,
getSqlExpressionResolver(),

View File

@ -21,7 +21,6 @@ import org.hibernate.query.sqm.tree.cte.SqmCteStatement;
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
import org.hibernate.query.sqm.tree.predicate.SqmWhereClause;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlAstTreeHelper;
import org.hibernate.sql.ast.tree.cte.CteStatement;
@ -82,7 +81,7 @@ public class StandardSqmDeleteTranslator
final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(
rootPath,
null,
JoinType.LEFT,
false,
LockMode.WRITE,
stem -> getSqlAliasBaseGenerator().createSqlAliasBase( stem ),
getSqlExpressionResolver(),

View File

@ -18,7 +18,6 @@ import org.hibernate.query.sqm.sql.SqmInsertSelectTranslation;
import org.hibernate.query.sqm.sql.SqmInsertSelectTranslator;
import org.hibernate.query.sqm.tree.cte.SqmCteStatement;
import org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.tree.cte.CteStatement;
import org.hibernate.sql.ast.tree.from.TableGroup;
@ -69,7 +68,7 @@ public class StandardSqmInsertSelectTranslator
final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(
rootPath,
null,
JoinType.LEFT,
false,
LockMode.WRITE,
stem -> getSqlAliasBaseGenerator().createSqlAliasBase( stem ),
getSqlExpressionResolver(),

View File

@ -43,7 +43,7 @@ import org.hibernate.query.sqm.tree.select.SqmDynamicInstantiationTarget;
import org.hibernate.query.sqm.tree.select.SqmQuerySpec;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.select.SqmSelection;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.FromClauseAccess;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
@ -299,7 +299,7 @@ public class StandardSqmSelectTranslator
fetchablePath,
lhs,
alias,
JoinType.LEFT,
SqlAstJoinType.LEFT,
LockMode.NONE,
getSqlAliasBaseManager(),
getSqlExpressionResolver(),

View File

@ -29,7 +29,6 @@ import org.hibernate.query.sqm.tree.update.SqmAssignment;
import org.hibernate.query.sqm.tree.update.SqmSetClause;
import org.hibernate.query.sqm.tree.update.SqmUpdateStatement;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlTreeCreationLogger;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
@ -39,12 +38,12 @@ import org.hibernate.sql.ast.spi.SqlAstTreeHelper;
import org.hibernate.sql.ast.tree.cte.CteStatement;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.ast.tree.update.Assignment;
import org.hibernate.sql.ast.tree.update.UpdateStatement;
import org.hibernate.sql.exec.internal.JdbcParameterImpl;
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.type.spi.TypeConfiguration;
/**
@ -96,7 +95,7 @@ public class StandardSqmUpdateTranslator
final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(
rootPath,
null,
JoinType.LEFT,
false,
LockMode.WRITE,
getSqlAliasBaseGenerator(),
getSqlExpressionResolver(),

View File

@ -6,7 +6,7 @@
*/
package org.hibernate.query.sqm.tree;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
/**
* Represents a canonical join type.
@ -24,38 +24,38 @@ public enum SqmJoinType {
/**
* Represents an inner join.
*/
INNER( "inner", JoinType.INNER, javax.persistence.criteria.JoinType.INNER ),
INNER( "inner", SqlAstJoinType.INNER, javax.persistence.criteria.JoinType.INNER ),
/**
* Represents a left outer join.
*/
LEFT( "left outer", JoinType.LEFT, javax.persistence.criteria.JoinType.LEFT ),
LEFT( "left outer", SqlAstJoinType.LEFT, javax.persistence.criteria.JoinType.LEFT ),
/**
* Represents a right outer join.
*/
RIGHT( "right outer", JoinType.RIGHT, javax.persistence.criteria.JoinType.RIGHT ),
RIGHT( "right outer", SqlAstJoinType.RIGHT, javax.persistence.criteria.JoinType.RIGHT ),
/**
* Represents a cross join (aka a cartesian product).
*/
CROSS( "cross", JoinType.CROSS, null ),
CROSS( "cross", SqlAstJoinType.CROSS, null ),
/**
* Represents a full join.
*/
FULL( "full", JoinType.FULL, null );
FULL( "full", SqlAstJoinType.FULL, null );
private final String text;
private final JoinType correspondingSqlJoinType;
private final SqlAstJoinType correspondingSqlAstJoinType;
private final javax.persistence.criteria.JoinType correspondingJpaJoinType;
SqmJoinType(
String text,
JoinType correspondingSqlJoinType,
SqlAstJoinType correspondingSqlAstJoinType,
javax.persistence.criteria.JoinType correspondingJpaJoinType) {
this.text = text;
this.correspondingSqlJoinType = correspondingSqlJoinType;
this.correspondingSqlAstJoinType = correspondingSqlAstJoinType;
this.correspondingJpaJoinType = correspondingJpaJoinType;
}
@ -68,8 +68,8 @@ public enum SqmJoinType {
return text;
}
public JoinType getCorrespondingSqlJoinType() {
return correspondingSqlJoinType;
public SqlAstJoinType getCorrespondingSqlJoinType() {
return correspondingSqlAstJoinType;
}
public javax.persistence.criteria.JoinType getCorrespondingJpaJoinType() {

View File

@ -9,7 +9,7 @@ package org.hibernate.sql.ast;
/**
* @author Steve Ebersole
*/
public enum JoinType {
public enum SqlAstJoinType {
/**
* Represents an inner join.
*/
@ -37,7 +37,7 @@ public enum JoinType {
private final String text;
JoinType(String text) {
SqlAstJoinType(String text) {
this.text = text;
}

View File

@ -89,6 +89,7 @@ public class CteTable {
tableValueConstructorReference,
Collections.emptyList(),
null,
null,
sessionFactory
);

View File

@ -57,6 +57,14 @@ public class CteTableGroup implements TableGroup {
return Collections.emptySet();
}
@Override
public TableReference getTableReference(String tableExpression) {
if ( cteTableReference.getTableExpression().equals( tableExpression ) ) {
return cteTableReference;
}
return null;
}
@Override
public TableReference resolveTableReference(String tableExpression) {
return cteTableReference;

View File

@ -46,6 +46,21 @@ public abstract class AbstractColumnReferenceQualifier implements ColumnReferenc
return tableReference;
}
@Override
public TableReference getTableReference(String tableExpression) {
if ( getPrimaryTableReference().getTableExpression().equals( tableExpression ) ) {
return getPrimaryTableReference();
}
for ( TableReferenceJoin tableJoin : getTableReferenceJoins() ) {
if ( tableJoin.getJoinedTableReference().getTableExpression().equals( tableExpression ) ) {
return tableJoin.getJoinedTableReference();
}
}
return null;
}
protected TableReference resolveTableReferenceInternal(String tableExpression) {
if ( getPrimaryTableReference().getTableExpression().equals( tableExpression ) ) {
return getPrimaryTableReference();

View File

@ -9,6 +9,7 @@ package org.hibernate.sql.ast.tree.from;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import org.hibernate.LockMode;
@ -28,6 +29,7 @@ public abstract class AbstractTableGroup extends AbstractColumnReferenceQualifie
private Set<TableGroupJoin> tableGroupJoins;
private boolean isInnerJoinPossible;
private final SessionFactoryImplementor sessionFactory;
@SuppressWarnings("WeakerAccess")

View File

@ -8,13 +8,11 @@ package org.hibernate.sql.ast.tree.from;
import java.util.function.Supplier;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
/**
* @author Steve Ebersole
*/
public interface ColumnReferenceQualifier {
TableReference resolveTableReference(String tableExpression, Supplier<TableReference> creator);
TableReference resolveTableReference(String tableExpression);
TableReference getTableReference(String tableExpression);
}

View File

@ -114,6 +114,11 @@ public class CompositeTableGroup implements VirtualTableGroup {
return underlyingTableGroup.isInnerJoinPossible();
}
@Override
public TableReference getTableReference(String tableExpression) {
return underlyingTableGroup.getTableReference( tableExpression );
}
@Override
public TableReference resolveTableReference(
String tableExpression,

View File

@ -17,6 +17,9 @@ import org.hibernate.metamodel.mapping.ModelPartContainer;
import org.hibernate.query.NavigablePath;
/**
* Acts as a TableGroup for DML query operations. It is used to simply
* wrap the TableReference of the "mutating table"
*
* @author Steve Ebersole
*/
public class MutatingTableReferenceGroupWrapper implements VirtualTableGroup {
@ -53,6 +56,13 @@ public class MutatingTableReferenceGroupWrapper implements VirtualTableGroup {
return mutatingTableReference;
}
@Override
public TableReference getTableReference(String tableExpression) {
return mutatingTableReference.getTableExpression().equals( tableExpression )
? mutatingTableReference
: null;
}
@Override
public TableReference resolveTableReference(String tableExpression, Supplier<TableReference> creator) {
return resolveTableReference( tableExpression );
@ -60,9 +70,7 @@ public class MutatingTableReferenceGroupWrapper implements VirtualTableGroup {
@Override
public TableReference resolveTableReference(String tableExpression) {
return mutatingTableReference.getTableExpression().equals( tableExpression )
? mutatingTableReference
: null;
return getTableReference( tableExpression );
}
@Override

View File

@ -12,7 +12,6 @@ import java.util.function.Supplier;
import org.hibernate.LockMode;
import org.hibernate.metamodel.mapping.ModelPartContainer;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
@ -31,7 +30,7 @@ public interface RootTableGroupProducer extends TableGroupProducer, ModelPartCon
TableGroup createRootTableGroup(
NavigablePath navigablePath,
String explicitSourceAlias,
JoinType tableReferenceJoinType,
boolean canUseInnerJoins,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,

View File

@ -6,7 +6,10 @@
*/
package org.hibernate.sql.ast.tree.from;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import org.hibernate.LockMode;
@ -19,7 +22,9 @@ import org.hibernate.sql.ast.spi.SqlAliasBase;
*/
public class StandardTableGroup extends AbstractTableGroup {
private final TableReference primaryTableReference;
private final List<TableReferenceJoin> tableJoins;
private final BiFunction<String,TableGroup,TableReferenceJoin> tableReferenceJoinCreator;
private List<TableReferenceJoin> tableJoins;
public StandardTableGroup(
NavigablePath navigablePath,
@ -28,10 +33,12 @@ public class StandardTableGroup extends AbstractTableGroup {
TableReference primaryTableReference,
List<TableReferenceJoin> tableJoins,
SqlAliasBase sqlAliasBase,
BiFunction<String,TableGroup,TableReferenceJoin> tableReferenceJoinCreator,
SessionFactoryImplementor sessionFactory) {
super( navigablePath, tableGroupProducer, lockMode, sqlAliasBase, sessionFactory );
this.primaryTableReference = primaryTableReference;
this.tableJoins = tableJoins;
this.tableReferenceJoinCreator = tableReferenceJoinCreator;
}
@Override
@ -50,18 +57,23 @@ public class StandardTableGroup extends AbstractTableGroup {
@Override
public List<TableReferenceJoin> getTableReferenceJoins() {
return tableJoins;
return tableJoins == null ? Collections.emptyList() : tableJoins;
}
@Override
public TableReference resolveTableReferenceInternal(String tableExpression) {
TableReference tableReference = super.resolveTableReferenceInternal( tableExpression );
final TableReference tableReference = super.resolveTableReferenceInternal( tableExpression );
if ( tableReference != null ) {
return tableReference;
}
for ( TableReferenceJoin tableJoin : tableJoins ) {
if ( tableJoin.getJoinedTableReference().getTableExpression().equals( tableExpression ) ) {
return tableJoin.getJoinedTableReference();
if ( tableJoins != null ) {
for ( int i = 0; i < tableJoins.size(); i++ ) {
final TableReferenceJoin join = tableJoins.get( i );
assert join != null;
if ( join.getJoinedTableReference().getTableExpression().equals( tableExpression ) ) {
return join.getJoinedTableReference();
}
}
}
@ -71,6 +83,21 @@ public class StandardTableGroup extends AbstractTableGroup {
return primaryTableReference;
}
}
return potentiallyCreateTableReference( tableExpression );
}
@SuppressWarnings("WeakerAccess")
protected TableReference potentiallyCreateTableReference(String tableExpression) {
final TableReferenceJoin join = tableReferenceJoinCreator.apply( tableExpression, this );
if ( join != null ) {
if ( tableJoins == null ) {
tableJoins = new ArrayList<>();
}
tableJoins.add( join );
return join.getJoinedTableReference();
}
return null;
}
}

View File

@ -7,14 +7,12 @@
package org.hibernate.sql.ast.tree.from;
import java.util.ArrayList;
import java.util.Collections;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.hibernate.LockMode;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
/**
@ -26,22 +24,14 @@ public class TableGroupBuilder implements TableReferenceCollector {
TableGroupProducer producer,
LockMode lockMode,
SqlAliasBase sqlAliasBase,
BiFunction<String,TableGroup,TableReferenceJoin> tableReferenceJoinCreator,
SessionFactoryImplementor sessionFactory) {
return new TableGroupBuilder( path, producer, lockMode, sqlAliasBase, sessionFactory );
}
public static TableGroupBuilder builder(
NavigablePath path,
TableGroupProducer producer,
LockMode lockMode,
SqlAliasBase sqlAliasBase,
BiFunction<TableReference, TableReference,TableReferenceJoin> primaryJoinProducer,
SessionFactoryImplementor sessionFactory) {
return new TableGroupBuilder( path, producer, lockMode, sqlAliasBase, primaryJoinProducer, sessionFactory );
return new TableGroupBuilder( path, producer, lockMode, sqlAliasBase, tableReferenceJoinCreator, sessionFactory );
}
private final NavigablePath path;
private final TableGroupProducer producer;
private final BiFunction<String, TableGroup, TableReferenceJoin> tableReferenceJoinCreator;
private final SessionFactoryImplementor sessionFactory;
private final SqlAliasBase sqlAliasBase;
@ -59,25 +49,28 @@ public class TableGroupBuilder implements TableReferenceCollector {
TableGroupProducer producer,
LockMode lockMode,
SqlAliasBase sqlAliasBase,
SessionFactoryImplementor sessionFactory) {
this( path, producer, lockMode, sqlAliasBase, null, sessionFactory );
}
private TableGroupBuilder(
NavigablePath path,
TableGroupProducer producer,
LockMode lockMode,
SqlAliasBase sqlAliasBase,
BiFunction<TableReference, TableReference,TableReferenceJoin> primaryJoinProducer,
BiFunction<String, TableGroup, TableReferenceJoin> tableReferenceJoinCreator,
SessionFactoryImplementor sessionFactory) {
this.path = path;
this.producer = producer;
this.lockMode = lockMode;
this.sqlAliasBase = sqlAliasBase;
this.primaryJoinProducer = primaryJoinProducer;
this.tableReferenceJoinCreator = tableReferenceJoinCreator;
this.sessionFactory = sessionFactory;
}
public NavigablePath getPath() {
return path;
}
public SessionFactoryImplementor getSessionFactory() {
return sessionFactory;
}
public SqlAliasBase getSqlAliasBase() {
return sqlAliasBase;
}
@Override
public void applyPrimaryJoinProducer(BiFunction<TableReference, TableReference, TableReferenceJoin> primaryJoinProducer) {
this.primaryJoinProducer = primaryJoinProducer;
@ -93,8 +86,9 @@ public class TableGroupBuilder implements TableReferenceCollector {
producer,
lockMode,
primaryTableReference,
tableJoins == null ? Collections.emptyList() : tableJoins,
tableJoins,
sqlAliasBase,
tableReferenceJoinCreator,
sessionFactory
);
}
@ -115,7 +109,7 @@ public class TableGroupBuilder implements TableReferenceCollector {
@Override
public void applySecondaryTableReferences(
TableReference tableReference,
JoinType tableReferenceJoinType,
SqlAstJoinType tableReferenceSqlAstJoinType,
TableReferenceJoinPredicateProducer predicateProducer) {
if ( primaryTableReference == null ) {
primaryTableReference = tableReference;
@ -124,12 +118,12 @@ public class TableGroupBuilder implements TableReferenceCollector {
else {
addTableReferenceJoin(
new TableReferenceJoin(
tableReferenceJoinType,
tableReferenceSqlAstJoinType,
tableReference,
predicateProducer.producePredicate(
secondaryTableLhs,
tableReference,
tableReferenceJoinType
tableReferenceSqlAstJoinType
)
)
);

View File

@ -7,7 +7,7 @@
package org.hibernate.sql.ast.tree.from;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAstTreeHelper;
import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.tree.SqlAstNode;
@ -21,31 +21,31 @@ import org.hibernate.query.sqm.sql.internal.DomainResultProducer;
*/
public class TableGroupJoin implements SqlAstNode, DomainResultProducer {
private final NavigablePath navigablePath;
private final JoinType joinType;
private final SqlAstJoinType sqlAstJoinType;
private final TableGroup joinedGroup;
private Predicate predicate;
public TableGroupJoin(
NavigablePath navigablePath,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
TableGroup joinedGroup,
Predicate predicate) {
this.navigablePath = navigablePath;
this.joinType = joinType;
this.sqlAstJoinType = sqlAstJoinType;
this.joinedGroup = joinedGroup;
this.predicate = predicate;
}
public TableGroupJoin(
NavigablePath navigablePath,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
TableGroup joinedGroup) {
this( navigablePath, joinType, joinedGroup, null );
this( navigablePath, sqlAstJoinType, joinedGroup, null );
}
public JoinType getJoinType() {
return joinType;
public SqlAstJoinType getJoinType() {
return sqlAstJoinType;
}
public TableGroup getJoinedGroup() {

View File

@ -8,7 +8,7 @@ package org.hibernate.sql.ast.tree.from;
import org.hibernate.LockMode;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBaseGenerator;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
@ -24,7 +24,7 @@ public interface TableGroupJoinProducer extends TableGroupProducer {
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
LockMode lockMode,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,

View File

@ -71,6 +71,14 @@ public class TableReference implements SqlAstNode, ColumnReferenceQualifier {
return null;
}
@Override
public TableReference getTableReference(String tableExpression) {
if ( this.tableExpression.equals( tableExpression ) ) {
return this;
}
return null;
}
@Override
public String toString() {
return getTableExpression() + "(" + getIdentificationVariable() + ')';

View File

@ -8,7 +8,7 @@ package org.hibernate.sql.ast.tree.from;
import java.util.function.BiFunction;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
/**
* Used in constructing {@link TableGroup} references to collect the individual table
@ -25,14 +25,14 @@ public interface TableReferenceCollector {
* Collect a table reference as part of the TableGroup.
*
* @param tableReference The TableReference.
* @param joinType The type of join indicated by the mapping of the table, if it is to be joined
* @param sqlAstJoinType The type of join indicated by the mapping of the table, if it is to be joined
* @param predicateProducer Function for creating the join predicate, if it is to be joined. The first
* argument passed to the function is the LHS reference. The second is the same as `tableReference`.
* The result is a SQL AST Predicate to use as the join-predicate
*/
void applySecondaryTableReferences(
TableReference tableReference,
JoinType joinType,
SqlAstJoinType sqlAstJoinType,
TableReferenceJoinPredicateProducer predicateProducer);
/**

View File

@ -7,7 +7,7 @@
package org.hibernate.sql.ast.tree.from;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
@ -20,8 +20,25 @@ public interface TableReferenceContributor {
*/
void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext);
default TableReference createPrimaryTableReference(
SqlAliasBase sqlAliasBase,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
throw new UnsupportedOperationException( "Contributor [" + getClass().getName() + "] does not support primary TableReference creation" );
}
default TableReferenceJoin createTableReferenceJoin(
String joinTableExpression,
SqlAliasBase sqlAliasBase,
TableReference lhs,
boolean canUseInnerJoin,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
throw new UnsupportedOperationException( "Contributor [" + getClass().getName() + "] does not support TableReference join creation" );
}
}

View File

@ -6,7 +6,7 @@
*/
package org.hibernate.sql.ast.tree.from;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.sql.ast.tree.predicate.Predicate;
@ -17,12 +17,12 @@ import org.hibernate.sql.ast.tree.predicate.Predicate;
* @author Steve Ebersole
*/
public class TableReferenceJoin implements SqlAstNode {
private final JoinType joinType;
private final SqlAstJoinType sqlAstJoinType;
private final TableReference joinedTableBinding;
private final Predicate predicate;
public TableReferenceJoin(JoinType joinType, TableReference joinedTableBinding, Predicate predicate) {
this.joinType = joinType == null ? JoinType.LEFT : joinType;
public TableReferenceJoin(SqlAstJoinType sqlAstJoinType, TableReference joinedTableBinding, Predicate predicate) {
this.sqlAstJoinType = sqlAstJoinType == null ? SqlAstJoinType.LEFT : sqlAstJoinType;
this.joinedTableBinding = joinedTableBinding;
this.predicate = predicate;
@ -33,8 +33,8 @@ public class TableReferenceJoin implements SqlAstNode {
// }
}
public JoinType getJoinType() {
return joinType;
public SqlAstJoinType getJoinType() {
return sqlAstJoinType;
}
public TableReference getJoinedTableReference() {

View File

@ -6,7 +6,7 @@
*/
package org.hibernate.sql.ast.tree.from;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.tree.predicate.Predicate;
/**
@ -20,5 +20,5 @@ import org.hibernate.sql.ast.tree.predicate.Predicate;
@FunctionalInterface
public
interface TableReferenceJoinPredicateProducer {
Predicate producePredicate(TableReference lhs, TableReference rhs, JoinType joinType);
Predicate producePredicate(TableReference lhs, TableReference rhs, SqlAstJoinType sqlAstJoinType);
}

View File

@ -13,7 +13,6 @@ import java.util.function.Consumer;
import java.util.function.Supplier;
import org.hibernate.LockMode;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.metamodel.mapping.ModelPartContainer;
import org.hibernate.persister.entity.UnionSubclassEntityPersister;
import org.hibernate.query.NavigablePath;
@ -98,13 +97,20 @@ public class UnionTableGroup implements VirtualTableGroup {
}
@Override
public TableReference resolveTableReference(
String tableExpression, Supplier<TableReference> creator) {
public TableReference getTableReference(String tableExpression) {
assert tableReference.getTableExpression().equals( tableExpression );
return tableReference;
}
@Override
public TableReference resolveTableReference(String tableExpression, Supplier<TableReference> creator) {
assert tableReference.getTableExpression().equals( tableExpression );
return tableReference;
}
@Override
public TableReference resolveTableReference(String tableExpression) {
// assert tableReference.getTableExpression().equals( tableExpression );
return tableReference;
}
}

View File

@ -102,6 +102,11 @@ public class EntityCollectionPartTableGroup implements TableGroup {
return collectionTableGroup.isInnerJoinPossible();
}
@Override
public TableReference getTableReference(String tableExpression) {
return collectionTableGroup.getTableReference( tableExpression );
}
@Override
public TableReference resolveTableReference(String tableExpression, Supplier<TableReference> creator) {
return collectionTableGroup.resolveTableReference( tableExpression, creator );

View File

@ -14,7 +14,7 @@ import org.hibernate.sql.results.graph.embeddable.EmbeddableValuedFetchable;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.results.graph.AbstractFetchParent;
import org.hibernate.sql.results.graph.AssemblerCreationState;
@ -57,7 +57,7 @@ public class EmbeddableFetchImpl extends AbstractFetchParent implements Embeddab
.getFromClauseAccess()
.findTableGroup( fetchParent.getNavigablePath() ),
null,
nullable ? JoinType.LEFT : JoinType.INNER,
nullable ? SqlAstJoinType.LEFT : SqlAstJoinType.INNER,
LockMode.NONE,
stem -> creationState.getSqlAliasBaseManager().createSqlAliasBase( stem ),
creationState.getSqlAstCreationState().getSqlExpressionResolver(),

View File

@ -12,7 +12,7 @@ import org.hibernate.LockMode;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.FromClauseAccess;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.results.graph.AbstractFetchParent;
@ -48,7 +48,7 @@ public class EmbeddableResultImpl<T> extends AbstractFetchParent implements Embe
navigablePath,
fromClauseAccess.findTableGroup( navigablePath.getParent() ),
resultVariable,
JoinType.INNER,
SqlAstJoinType.INNER,
LockMode.NONE,
creationState.getSqlAstCreationState().getSqlAliasBaseGenerator(),
creationState.getSqlAstCreationState().getSqlExpressionResolver(),

View File

@ -60,7 +60,7 @@ import org.hibernate.persister.spi.PersisterCreationContext;
import org.hibernate.persister.walking.spi.AttributeDefinition;
import org.hibernate.persister.walking.spi.EntityIdentifierDefinition;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.tree.from.TableReferenceCollector;
@ -727,7 +727,7 @@ public class PersisterClassProviderTest {
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {

View File

@ -60,7 +60,7 @@ import org.hibernate.persister.walking.spi.CollectionElementDefinition;
import org.hibernate.persister.walking.spi.CollectionIndexDefinition;
import org.hibernate.persister.walking.spi.EntityIdentifierDefinition;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.tree.from.TableReferenceCollector;
@ -713,7 +713,7 @@ public class GoofyPersisterClassProvider implements PersisterClassResolver {
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {

View File

@ -60,7 +60,7 @@ import org.hibernate.persister.entity.MultiLoadOptions;
import org.hibernate.persister.spi.PersisterCreationContext;
import org.hibernate.persister.walking.spi.AttributeDefinition;
import org.hibernate.persister.walking.spi.EntityIdentifierDefinition;
import org.hibernate.sql.ast.JoinType;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
@ -826,7 +826,7 @@ public class CustomPersister implements EntityPersister {
@Override
public void applyTableReferences(
SqlAliasBase sqlAliasBase,
JoinType baseJoinType,
SqlAstJoinType baseSqlAstJoinType,
TableReferenceCollector collector,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {