From 4e9b8c0c344f23c1b38d1f26fb495cd8c3bd184b Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Mon, 10 May 2021 16:45:40 +0200 Subject: [PATCH] Introduce ForeignKeyDescriptor.Side wrapper around ModelPart of the FK sides to discriminate the role of the side better --- .../internal/LoaderSqlAstCreationState.java | 6 +- .../metamodel/mapping/Association.java | 2 +- .../mapping/ForeignKeyDescriptor.java | 16 ++- .../EmbeddedForeignKeyDescriptor.java | 91 ++++++------ .../EmbeddedForeignKeyDescriptorSide.java | 36 +++++ .../internal/EntityCollectionPart.java | 4 +- .../internal/SimpleForeignKeyDescriptor.java | 132 ++++++++++-------- .../SimpleForeignKeyDescriptorSide.java | 36 +++++ .../internal/ToOneAttributeMapping.java | 34 ++--- .../DomainResultCreationStateImpl.java | 6 +- .../sqm/sql/BaseSqmToSqlAstConverter.java | 6 +- .../EntityValuedPathInterpretation.java | 2 +- .../graph/DomainResultCreationState.java | 4 +- .../CircularBiDirectionalFetchImpl.java | 4 +- .../internal/domain/CircularFetchImpl.java | 4 +- 15 files changed, 245 insertions(+), 138 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedForeignKeyDescriptorSide.java create mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SimpleForeignKeyDescriptorSide.java diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSqlAstCreationState.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSqlAstCreationState.java index 505de74185..c3b0ab7fdb 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSqlAstCreationState.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSqlAstCreationState.java @@ -57,7 +57,7 @@ public class LoaderSqlAstCreationState private final FetchProcessor fetchProcessor; private boolean resolvingCircularFetch; - private ForeignKeyDescriptor.Side currentlyResolvingForeignKeySide; + private ForeignKeyDescriptor.Nature currentlyResolvingForeignKeySide; private Set visitedAssociationKeys = new HashSet<>(); public LoaderSqlAstCreationState( @@ -128,12 +128,12 @@ public class LoaderSqlAstCreationState } @Override - public ForeignKeyDescriptor.Side getCurrentlyResolvingForeignKeyPart() { + public ForeignKeyDescriptor.Nature getCurrentlyResolvingForeignKeyPart() { return currentlyResolvingForeignKeySide; } @Override - public void setCurrentlyResolvingForeignKeyPart(ForeignKeyDescriptor.Side currentlyResolvingForeignKeySide) { + public void setCurrentlyResolvingForeignKeyPart(ForeignKeyDescriptor.Nature currentlyResolvingForeignKeySide) { this.currentlyResolvingForeignKeySide = currentlyResolvingForeignKeySide; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/Association.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/Association.java index 1826d27bec..a8360d5a3f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/Association.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/Association.java @@ -13,5 +13,5 @@ import org.hibernate.sql.results.graph.Fetchable; */ public interface Association extends Fetchable { ForeignKeyDescriptor getForeignKeyDescriptor(); - ForeignKeyDescriptor.Side getSide(); + ForeignKeyDescriptor.Nature getSideNature(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ForeignKeyDescriptor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ForeignKeyDescriptor.java index 105255ec4a..8bed0d280a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ForeignKeyDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ForeignKeyDescriptor.java @@ -25,11 +25,19 @@ import org.hibernate.sql.results.graph.DomainResultCreationState; */ public interface ForeignKeyDescriptor extends VirtualModelPart { - enum Side { + enum Nature { KEY, TARGET; } + interface Side { + + Nature getNature(); + + ModelPart getModelPart(); + + } + String PART_NAME = "{fk}"; String getKeyTable(); @@ -40,6 +48,10 @@ public interface ForeignKeyDescriptor extends VirtualModelPart { ModelPart getTargetPart(); + Side getKeySide(); + + Side getTargetSide(); + /** * Create a DomainResult for the referring-side of the fk */ @@ -64,7 +76,7 @@ public interface ForeignKeyDescriptor extends VirtualModelPart { DomainResult createDomainResult( NavigablePath navigablePath, TableGroup tableGroup, - Side side, + Nature side, DomainResultCreationState creationState); Predicate generateJoinPredicate( diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedForeignKeyDescriptor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedForeignKeyDescriptor.java index ba1c5484a6..61ae42db3f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedForeignKeyDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedForeignKeyDescriptor.java @@ -52,8 +52,8 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor; */ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { - private final EmbeddableValuedModelPart keyMappingType; - private final EmbeddableValuedModelPart targetMappingType; + private final EmbeddedForeignKeyDescriptorSide keySide; + private final EmbeddedForeignKeyDescriptorSide targetSide; private final String keyTable; private final SelectableMappings keySelectableMappings; private final String targetTable; @@ -72,8 +72,8 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { this.keySelectableMappings = keySelectableMappings; this.targetTable = targetTable; this.targetSelectableMappings = targetSelectableMappings; - this.targetMappingType = targetMappingType; - this.keyMappingType = keyMappingType; + this.targetSide = new EmbeddedForeignKeyDescriptorSide( Nature.TARGET, targetMappingType ); + this.keySide = new EmbeddedForeignKeyDescriptorSide( Nature.KEY, keyMappingType ); final List columns = new ArrayList<>( keySelectableMappings.getJdbcTypeCount() ); keySelectableMappings.forEachSelectable( (columnIndex, selection) -> { @@ -106,11 +106,14 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { this.keySelectableMappings = keySelectableMappings; this.targetTable = original.targetTable; this.targetSelectableMappings = original.targetSelectableMappings; - this.targetMappingType = original.targetMappingType; - this.keyMappingType = EmbeddedAttributeMapping.createInverseModelPart( - targetMappingType, - keySelectableMappings, - creationProcess + this.targetSide = original.targetSide; + this.keySide = new EmbeddedForeignKeyDescriptorSide( + Nature.KEY, + EmbeddedAttributeMapping.createInverseModelPart( + original.targetSide.getModelPart(), + keySelectableMappings, + creationProcess + ) ); final List columns = new ArrayList<>( keySelectableMappings.getJdbcTypeCount() ); keySelectableMappings.forEachSelectable( @@ -131,6 +134,26 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { return targetTable; } + @Override + public ModelPart getKeyPart() { + return keySide.getModelPart().getEmbeddableTypeDescriptor().getEmbeddedValueMapping(); + } + + @Override + public ModelPart getTargetPart() { + return targetSide.getModelPart().getEmbeddableTypeDescriptor().getEmbeddedValueMapping(); + } + + @Override + public Side getKeySide() { + return keySide; + } + + @Override + public Side getTargetSide() { + return targetSide; + } + @Override public ForeignKeyDescriptor withKeySelectionMapping( IntFunction selectableMappingAccess, @@ -157,7 +180,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { tableGroup, null, keyTable, - keyMappingType, + keySide.getModelPart(), creationState ); } @@ -174,7 +197,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { tableGroup, null, targetTable, - targetMappingType, + targetSide.getModelPart(), creationState ); } @@ -190,7 +213,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { tableGroup, null, targetTable, - targetMappingType, + targetSide.getModelPart(), creationState ); } @@ -200,7 +223,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { tableGroup, null, keyTable, - keyMappingType, + keySide.getModelPart(), creationState ); } @@ -210,15 +233,15 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { public DomainResult createDomainResult( NavigablePath navigablePath, TableGroup tableGroup, - Side side, + Nature side, DomainResultCreationState creationState) { - if ( side == Side.KEY ) { + if ( side == Nature.KEY ) { return createDomainResult( navigablePath, tableGroup, null, keyTable, - keyMappingType, + keySide.getModelPart(), creationState ); } @@ -228,7 +251,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { tableGroup, null, targetTable, - targetMappingType, + targetSide.getModelPart(), creationState ); } @@ -240,7 +263,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { TableGroup tableGroup, String resultVariable, DomainResultCreationState creationState) { - return createDomainResult( navigablePath, tableGroup, resultVariable, keyTable, keyMappingType, creationState ); + return createDomainResult( navigablePath, tableGroup, resultVariable, keyTable, keySide.getModelPart(), creationState ); } private DomainResult createDomainResult( @@ -293,9 +316,9 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { ); } - final Side currentForeignKeyResolvingKey = creationState.getCurrentlyResolvingForeignKeyPart(); + final Nature currentForeignKeyResolvingKey = creationState.getCurrentlyResolvingForeignKeyPart(); try { - creationState.setCurrentlyResolvingForeignKeyPart( keyMappingType == modelPart ? Side.KEY : Side.TARGET ); + creationState.setCurrentlyResolvingForeignKeyPart( keySide.getModelPart() == modelPart ? Nature.KEY : Nature.TARGET ); return new EmbeddableForeignKeyResultImpl<>( resultNavigablePath, modelPart, @@ -444,29 +467,19 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { return associationKey; } - @Override - public ModelPart getKeyPart() { - return keyMappingType.getEmbeddableTypeDescriptor().getEmbeddedValueMapping(); - } - - @Override - public ModelPart getTargetPart() { - return targetMappingType.getEmbeddableTypeDescriptor().getEmbeddedValueMapping(); - } - @Override public MappingType getPartMappingType() { - return targetMappingType.getPartMappingType(); + return targetSide.getModelPart().getPartMappingType(); } @Override public JavaTypeDescriptor getJavaTypeDescriptor() { - return targetMappingType.getJavaTypeDescriptor(); + return targetSide.getModelPart().getJavaTypeDescriptor(); } @Override public NavigableRole getNavigableRole() { - return targetMappingType.getNavigableRole(); + return targetSide.getModelPart().getNavigableRole(); } @Override @@ -483,8 +496,8 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { @Override public Object getAssociationKeyFromTarget(Object targetObject, SharedSessionContractImplementor session) { // If the mapping type has an identifier type, that identifier is the key - if ( targetMappingType instanceof SingleAttributeIdentifierMapping ) { - return ( (SingleAttributeIdentifierMapping) targetMappingType ).getIdentifier( targetObject, session ); + if ( targetSide.getModelPart() instanceof SingleAttributeIdentifierMapping ) { + return ( (SingleAttributeIdentifierMapping) targetSide.getModelPart() ).getIdentifier( targetObject, session ); } // Otherwise this is a key based on the target object i.e. without id-class return targetObject; @@ -492,12 +505,12 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { @Override public EntityMappingType findContainingEntityMapping() { - return targetMappingType.findContainingEntityMapping(); + return targetSide.getModelPart().findContainingEntityMapping(); } @Override public int forEachJdbcType(int offset, IndexedConsumer action) { - return targetMappingType.forEachJdbcType( offset, action ); + return targetSide.getModelPart().forEachJdbcType( offset, action ); } @Override @@ -507,11 +520,11 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor { int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) { - return targetMappingType.forEachDisassembledJdbcValue( value, clause, offset, valuesConsumer, session ); + return targetSide.getModelPart().forEachDisassembledJdbcValue( value, clause, offset, valuesConsumer, session ); } @Override public Object disassemble(Object value, SharedSessionContractImplementor session) { - return targetMappingType.disassemble( value, session ); + return targetSide.getModelPart().disassemble( value, session ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedForeignKeyDescriptorSide.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedForeignKeyDescriptorSide.java new file mode 100644 index 0000000000..c6f7dbae7a --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddedForeignKeyDescriptorSide.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.metamodel.mapping.internal; + +import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; +import org.hibernate.metamodel.mapping.ForeignKeyDescriptor; + +/** + * @author Steve Ebersole + */ +public class EmbeddedForeignKeyDescriptorSide implements ForeignKeyDescriptor.Side { + + private final ForeignKeyDescriptor.Nature nature; + private final EmbeddableValuedModelPart modelPart; + + public EmbeddedForeignKeyDescriptorSide( + ForeignKeyDescriptor.Nature nature, + EmbeddableValuedModelPart modelPart) { + this.nature = nature; + this.modelPart = modelPart; + } + + @Override + public ForeignKeyDescriptor.Nature getNature() { + return nature; + } + + @Override + public EmbeddableValuedModelPart getModelPart() { + return modelPart; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EntityCollectionPart.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EntityCollectionPart.java index e8891707d7..f00a20534c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EntityCollectionPart.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EntityCollectionPart.java @@ -332,8 +332,8 @@ public class EntityCollectionPart } @Override - public ForeignKeyDescriptor.Side getSide() { - return ForeignKeyDescriptor.Side.TARGET; + public ForeignKeyDescriptor.Nature getSideNature() { + return ForeignKeyDescriptor.Nature.TARGET; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SimpleForeignKeyDescriptor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SimpleForeignKeyDescriptor.java index 1d5d30ae40..bcccc22dab 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SimpleForeignKeyDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SimpleForeignKeyDescriptor.java @@ -52,8 +52,8 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor; * @author Steve Ebersole */ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicValuedModelPart, FetchOptions { - private final BasicValuedModelPart keySide; - private final BasicValuedModelPart targetSide; + private final SimpleForeignKeyDescriptorSide keySide; + private final SimpleForeignKeyDescriptorSide targetSide; private final boolean refersToPrimaryKey; @@ -84,12 +84,12 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa keySelectableMapping ); if ( swapDirection ) { - this.keySide = targetModelPart; - this.targetSide = keyModelPart; + this.keySide = new SimpleForeignKeyDescriptorSide( Nature.KEY, targetModelPart ); + this.targetSide = new SimpleForeignKeyDescriptorSide( Nature.TARGET, keyModelPart ); } else { - this.keySide = keyModelPart; - this.targetSide = targetModelPart; + this.keySide = new SimpleForeignKeyDescriptorSide( Nature.KEY, keyModelPart ); + this.targetSide = new SimpleForeignKeyDescriptorSide( Nature.TARGET, targetModelPart ); } this.disassemblyValueExtractor = disassemblyValueExtractor; this.refersToPrimaryKey = refersToPrimaryKey; @@ -97,12 +97,32 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa @Override public String getKeyTable() { - return keySide.getContainingTableExpression(); + return keySide.getModelPart().getContainingTableExpression(); } @Override public String getTargetTable() { - return targetSide.getContainingTableExpression(); + return targetSide.getModelPart().getContainingTableExpression(); + } + + @Override + public BasicValuedModelPart getKeyPart() { + return keySide.getModelPart(); + } + + @Override + public BasicValuedModelPart getTargetPart() { + return targetSide.getModelPart(); + } + + @Override + public Side getKeySide() { + return keySide; + } + + @Override + public Side getTargetSide() { + return targetSide; } @Override @@ -111,7 +131,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa MappingModelCreationProcess creationProcess) { return new SimpleForeignKeyDescriptor( selectableMappingAccess.apply( 0 ), - targetSide, + targetSide.getModelPart(), disassemblyValueExtractor, refersToPrimaryKey ); @@ -122,24 +142,24 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa NavigablePath navigablePath, TableGroup tableGroup, DomainResultCreationState creationState) { - assert tableGroup.getTableReference( navigablePath, keySide.getContainingTableExpression() ) != null; + assert tableGroup.getTableReference( navigablePath, keySide.getModelPart().getContainingTableExpression() ) != null; return createDomainResult( navigablePath, tableGroup, - keySide, + keySide.getModelPart(), creationState ); } @Override public DomainResult createTargetDomainResult(NavigablePath navigablePath, TableGroup tableGroup, DomainResultCreationState creationState) { - assert tableGroup.getTableReference( navigablePath, targetSide.getContainingTableExpression() ) != null; + assert tableGroup.getTableReference( navigablePath, targetSide.getModelPart().getContainingTableExpression() ) != null; return createDomainResult( navigablePath, tableGroup, - targetSide, + targetSide.getModelPart(), creationState ); } @@ -149,20 +169,20 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa NavigablePath collectionPath, TableGroup tableGroup, DomainResultCreationState creationState) { - return createDomainResult( collectionPath, tableGroup, targetSide, creationState ); + return createDomainResult( collectionPath, tableGroup, targetSide.getModelPart(), creationState ); } @Override public DomainResult createDomainResult( NavigablePath navigablePath, TableGroup tableGroup, - Side side, + Nature side, DomainResultCreationState creationState) { - if ( side == Side.KEY ) { - return createDomainResult( navigablePath, tableGroup, keySide, creationState ); + if ( side == Nature.KEY ) { + return createDomainResult( navigablePath, tableGroup, keySide.getModelPart(), creationState ); } else { - return createDomainResult( navigablePath, tableGroup, targetSide, creationState ); + return createDomainResult( navigablePath, tableGroup, targetSide.getModelPart(), creationState ); } } @@ -172,7 +192,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa TableGroup tableGroup, String resultVariable, DomainResultCreationState creationState) { - return createDomainResult( navigablePath, tableGroup, keySide, creationState ); + return createDomainResult( navigablePath, tableGroup, keySide.getModelPart(), creationState ); } private DomainResult createDomainResult( @@ -221,17 +241,17 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa SqlAstJoinType sqlAstJoinType, SqlExpressionResolver sqlExpressionResolver, SqlAstCreationContext creationContext) { - if ( lhs.getTableReference( keySide.getContainingTableExpression() ) != null ) { + if ( lhs.getTableReference( keySide.getModelPart().getContainingTableExpression() ) != null ) { return new ComparisonPredicate( new ColumnReference( lhs, - keySide, + keySide.getModelPart(), creationContext.getSessionFactory() ), ComparisonOperator.EQUAL, new ColumnReference( rhs, - targetSide, + targetSide.getModelPart(), creationContext.getSessionFactory() ) ); @@ -240,13 +260,13 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa return new ComparisonPredicate( new ColumnReference( lhs, - targetSide, + targetSide.getModelPart(), creationContext.getSessionFactory() ), ComparisonOperator.EQUAL, new ColumnReference( rhs, - keySide, + keySide.getModelPart(), creationContext.getSessionFactory() ) ); @@ -262,22 +282,22 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa SqlAstCreationContext creationContext) { TableReference lhsTableReference; TableReference rhsTableKeyReference; - if ( targetSide.getContainingTableExpression().equals( keySide.getContainingTableExpression() ) ) { - lhsTableReference = getTableReferenceWhenTargetEqualsKey( lhs, tableGroup, keySide.getContainingTableExpression() ); + if ( targetSide.getModelPart().getContainingTableExpression().equals( keySide.getModelPart().getContainingTableExpression() ) ) { + lhsTableReference = getTableReferenceWhenTargetEqualsKey( lhs, tableGroup, keySide.getModelPart().getContainingTableExpression() ); rhsTableKeyReference = getTableReference( lhs, tableGroup, - targetSide.getContainingTableExpression() + targetSide.getModelPart().getContainingTableExpression() ); } else { - lhsTableReference = getTableReference( lhs, tableGroup, keySide.getContainingTableExpression() ); + lhsTableReference = getTableReference( lhs, tableGroup, keySide.getModelPart().getContainingTableExpression() ); rhsTableKeyReference = getTableReference( lhs, tableGroup, - targetSide.getContainingTableExpression() + targetSide.getModelPart().getContainingTableExpression() ); } @@ -324,34 +344,24 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa throw new IllegalStateException( "Could not resolve binding for table `" + table + "`" ); } - @Override - public BasicValuedModelPart getKeyPart() { - return keySide; - } - - @Override - public BasicValuedModelPart getTargetPart() { - return targetSide; - } - @Override public MappingType getPartMappingType() { - return targetSide.getMappedType(); + return targetSide.getModelPart().getMappedType(); } @Override public JavaTypeDescriptor getJavaTypeDescriptor() { - return targetSide.getJdbcMapping().getJavaTypeDescriptor(); + return targetSide.getModelPart().getJdbcMapping().getJavaTypeDescriptor(); } @Override public NavigableRole getNavigableRole() { - return targetSide.getNavigableRole(); + return targetSide.getModelPart().getNavigableRole(); } @Override public EntityMappingType findContainingEntityMapping() { - return targetSide.findContainingEntityMapping(); + return targetSide.getModelPart().findContainingEntityMapping(); } @Override @@ -383,38 +393,38 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa @Override public void breakDownJdbcValues(Object domainValue, JdbcValueConsumer valueConsumer, SharedSessionContractImplementor session) { - valueConsumer.consume( domainValue, keySide ); + valueConsumer.consume( domainValue, keySide.getModelPart() ); } @Override public int visitKeySelectables(int offset, SelectableConsumer consumer) { - consumer.accept( offset, keySide ); + consumer.accept( offset, keySide.getModelPart() ); return getJdbcTypeCount(); } @Override public int visitTargetSelectables(int offset, SelectableConsumer consumer) { - consumer.accept( offset, targetSide ); + consumer.accept( offset, targetSide.getModelPart() ); return getJdbcTypeCount(); } @Override public AssociationKey getAssociationKey() { if ( associationKey == null ) { - final List associationKeyColumns = Collections.singletonList( keySide.getSelectionExpression() ); - associationKey = new AssociationKey( keySide.getContainingTableExpression(), associationKeyColumns ); + final List associationKeyColumns = Collections.singletonList( keySide.getModelPart().getSelectionExpression() ); + associationKey = new AssociationKey( keySide.getModelPart().getContainingTableExpression(), associationKeyColumns ); } return associationKey; } @Override public List getJdbcMappings() { - return Collections.singletonList( targetSide.getJdbcMapping() ); + return Collections.singletonList( targetSide.getModelPart().getJdbcMapping() ); } @Override public int forEachJdbcType(int offset, IndexedConsumer action) { - action.accept( offset, targetSide.getJdbcMapping() ); + action.accept( offset, targetSide.getModelPart().getJdbcMapping() ); return getJdbcTypeCount(); } @@ -425,34 +435,34 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) { - valuesConsumer.consume( offset, value, targetSide.getJdbcMapping() ); + valuesConsumer.consume( offset, value, targetSide.getModelPart().getJdbcMapping() ); return getJdbcTypeCount(); } @Override public String getContainingTableExpression() { - return keySide.getContainingTableExpression(); + return keySide.getModelPart().getContainingTableExpression(); } @Override public String getSelectionExpression() { - return keySide.getSelectionExpression(); + return keySide.getModelPart().getSelectionExpression(); } @Override public boolean isFormula() { - return keySide.isFormula(); + return keySide.getModelPart().isFormula(); } @Override public String getCustomReadExpression() { - return keySide.getCustomReadExpression(); + return keySide.getModelPart().getCustomReadExpression(); } @Override public String getCustomWriteExpression() { - return keySide.getCustomWriteExpression(); + return keySide.getModelPart().getCustomWriteExpression(); } @Override @@ -494,17 +504,17 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa @Override public JdbcMapping getJdbcMapping() { - return keySide.getJdbcMapping(); + return keySide.getModelPart().getJdbcMapping(); } @Override public String toString() { return String.format( "SimpleForeignKeyDescriptor : %s.%s -> %s.%s", - keySide.getContainingTableExpression(), - keySide.getSelectionExpression(), - targetSide.getContainingTableExpression(), - targetSide.getSelectionExpression() + keySide.getModelPart().getContainingTableExpression(), + keySide.getModelPart().getSelectionExpression(), + targetSide.getModelPart().getContainingTableExpression(), + targetSide.getModelPart().getSelectionExpression() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SimpleForeignKeyDescriptorSide.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SimpleForeignKeyDescriptorSide.java new file mode 100644 index 0000000000..778d72b3f6 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SimpleForeignKeyDescriptorSide.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.metamodel.mapping.internal; + +import org.hibernate.metamodel.mapping.BasicValuedModelPart; +import org.hibernate.metamodel.mapping.ForeignKeyDescriptor; + +/** + * @author Steve Ebersole + */ +public class SimpleForeignKeyDescriptorSide implements ForeignKeyDescriptor.Side { + + private final ForeignKeyDescriptor.Nature nature; + private final BasicValuedModelPart modelPart; + + public SimpleForeignKeyDescriptorSide( + ForeignKeyDescriptor.Nature nature, + BasicValuedModelPart modelPart) { + this.nature = nature; + this.modelPart = modelPart; + } + + @Override + public ForeignKeyDescriptor.Nature getNature() { + return nature; + } + + @Override + public BasicValuedModelPart getModelPart() { + return modelPart; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java index bf452c8a59..23d2ed2e16 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java @@ -100,7 +100,7 @@ public class ToOneAttributeMapping private final TableGroupProducer declaringTableGroupProducer; private ForeignKeyDescriptor foreignKeyDescriptor; - private ForeignKeyDescriptor.Side lhsSide; + private ForeignKeyDescriptor.Nature sideNature; private String identifyingColumnsTableExpression; public ToOneAttributeMapping( @@ -286,9 +286,9 @@ public class ToOneAttributeMapping public void setForeignKeyDescriptor(ForeignKeyDescriptor foreignKeyDescriptor) { assert identifyingColumnsTableExpression != null; this.foreignKeyDescriptor = foreignKeyDescriptor; - this.lhsSide = foreignKeyDescriptor.getAssociationKey().getTable().equals( identifyingColumnsTableExpression ) - ? ForeignKeyDescriptor.Side.KEY - : ForeignKeyDescriptor.Side.TARGET; + this.sideNature = foreignKeyDescriptor.getAssociationKey().getTable().equals( identifyingColumnsTableExpression ) + ? ForeignKeyDescriptor.Nature.KEY + : ForeignKeyDescriptor.Nature.TARGET; } public void setIdentifyingColumnsTableExpression(String tableExpression) { @@ -301,12 +301,12 @@ public class ToOneAttributeMapping } @Override - public ForeignKeyDescriptor.Side getSide() { - return lhsSide; + public ForeignKeyDescriptor.Nature getSideNature() { + return sideNature; } public boolean canJoinForeignKey(EntityIdentifierMapping identifierMapping) { - return lhsSide == ForeignKeyDescriptor.Side.KEY && identifierMapping == getForeignKeyDescriptor().getTargetPart() && !isNullable; + return sideNature == ForeignKeyDescriptor.Nature.KEY && identifierMapping == getForeignKeyDescriptor().getTargetPart() && !isNullable; } public String getReferencedPropertyName() { @@ -474,7 +474,7 @@ public class ToOneAttributeMapping We have a cirularity but it is not bidirectional */ - if ( lhsSide == ForeignKeyDescriptor.Side.KEY ) { + if ( sideNature == ForeignKeyDescriptor.Nature.KEY ) { final TableGroup parentTableGroup = creationState .getSqlAstCreationState() .getFromClauseAccess() @@ -641,15 +641,15 @@ public class ToOneAttributeMapping */ - final ForeignKeyDescriptor.Side resolvingKeySideOfForeignKey = creationState.getCurrentlyResolvingForeignKeyPart(); - final ForeignKeyDescriptor.Side side; - if ( resolvingKeySideOfForeignKey == ForeignKeyDescriptor.Side.KEY && this.lhsSide == ForeignKeyDescriptor.Side.TARGET ) { + final ForeignKeyDescriptor.Nature resolvingKeySideOfForeignKey = creationState.getCurrentlyResolvingForeignKeyPart(); + final ForeignKeyDescriptor.Nature side; + if ( resolvingKeySideOfForeignKey == ForeignKeyDescriptor.Nature.KEY && this.sideNature == ForeignKeyDescriptor.Nature.TARGET ) { // If we are currently resolving the key part of a foreign key we do not want to add joins. // So if the lhs of this association is the target of the FK, we have to use the KEY part to avoid a join - side = ForeignKeyDescriptor.Side.KEY; + side = ForeignKeyDescriptor.Nature.KEY; } else { - side = this.lhsSide; + side = this.sideNature; } final DomainResult keyResult = foreignKeyDescriptor.createDomainResult( fetchablePath, @@ -658,7 +658,7 @@ public class ToOneAttributeMapping creationState ); boolean selectByUniqueKey; - if ( side == ForeignKeyDescriptor.Side.KEY ) { + if ( side == ForeignKeyDescriptor.Nature.KEY ) { // case 1.2 selectByUniqueKey = false; } @@ -695,7 +695,7 @@ public class ToOneAttributeMapping DomainResultCreationState creationState) { // We only need a join if the key is on the referring side i.e. this is an inverse to-one // and if the FK refers to a non-PK, in which case we must load the whole entity - if ( lhsSide == ForeignKeyDescriptor.Side.TARGET || referencedPropertyName != null ) { + if ( sideNature == ForeignKeyDescriptor.Nature.TARGET || referencedPropertyName != null ) { final TableGroupJoin tableGroupJoin = createTableGroupJoin( navigablePath, tableGroup, @@ -781,7 +781,7 @@ public class ToOneAttributeMapping final SqlAliasBase sqlAliasBase = aliasBaseGenerator.createSqlAliasBase( aliasRoot ); // We can only use the parent table group if the FK is located there // If this is false, the FK is on a join table - final boolean canUseParentTableGroup = lhsSide == ForeignKeyDescriptor.Side.KEY + final boolean canUseParentTableGroup = sideNature == ForeignKeyDescriptor.Nature.KEY && declaringTableGroupProducer.containsTableReference( identifyingColumnsTableExpression ); final LazyTableGroup lazyTableGroup = new LazyTableGroup( navigablePath, @@ -921,7 +921,7 @@ public class ToOneAttributeMapping @Override public int forEachSelectable(int offset, SelectableConsumer consumer) { - if ( lhsSide == ForeignKeyDescriptor.Side.KEY ) { + if ( sideNature == ForeignKeyDescriptor.Nature.KEY ) { return foreignKeyDescriptor.visitKeySelectables( offset, consumer ); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/query/results/DomainResultCreationStateImpl.java b/hibernate-core/src/main/java/org/hibernate/query/results/DomainResultCreationStateImpl.java index 14e2e626a0..2e972a90f4 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/results/DomainResultCreationStateImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/results/DomainResultCreationStateImpl.java @@ -77,7 +77,7 @@ public class DomainResultCreationStateImpl private final Stack relativePathStack = new StandardStack<>(); private boolean processingKeyFetches = false; private boolean resolvingCircularFetch; - private ForeignKeyDescriptor.Side currentlyResolvingForeignKeySide; + private ForeignKeyDescriptor.Nature currentlyResolvingForeignKeySide; public DomainResultCreationStateImpl( String stateIdentifier, @@ -422,12 +422,12 @@ public class DomainResultCreationStateImpl } @Override - public ForeignKeyDescriptor.Side getCurrentlyResolvingForeignKeyPart() { + public ForeignKeyDescriptor.Nature getCurrentlyResolvingForeignKeyPart() { return currentlyResolvingForeignKeySide; } @Override - public void setCurrentlyResolvingForeignKeyPart(ForeignKeyDescriptor.Side currentlyResolvingForeignKeySide) { + public void setCurrentlyResolvingForeignKeyPart(ForeignKeyDescriptor.Nature currentlyResolvingForeignKeySide) { this.currentlyResolvingForeignKeySide = currentlyResolvingForeignKeySide; } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index 6064825f80..0c4468806b 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -339,7 +339,7 @@ public abstract class BaseSqmToSqlAstConverter extends Base private int fetchDepth; private boolean resolvingCircularFetch; - private ForeignKeyDescriptor.Side currentlyResolvingForeignKeySide; + private ForeignKeyDescriptor.Nature currentlyResolvingForeignKeySide; private Map collectionFilterPredicates; private OrderByFragmentConsumer orderByFragmentConsumer; @@ -4701,12 +4701,12 @@ public abstract class BaseSqmToSqlAstConverter extends Base } @Override - public ForeignKeyDescriptor.Side getCurrentlyResolvingForeignKeyPart() { + public ForeignKeyDescriptor.Nature getCurrentlyResolvingForeignKeyPart() { return currentlyResolvingForeignKeySide; } @Override - public void setCurrentlyResolvingForeignKeyPart(ForeignKeyDescriptor.Side currentlyResolvingForeignKeySide) { + public void setCurrentlyResolvingForeignKeyPart(ForeignKeyDescriptor.Nature currentlyResolvingForeignKeySide) { this.currentlyResolvingForeignKeySide = currentlyResolvingForeignKeySide; } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/internal/EntityValuedPathInterpretation.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/internal/EntityValuedPathInterpretation.java index c3cd0e59f8..f45dc3d2b2 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/internal/EntityValuedPathInterpretation.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/internal/EntityValuedPathInterpretation.java @@ -66,7 +66,7 @@ public class EntityValuedPathInterpretation extends AbstractSqmPathInterpreta final ForeignKeyDescriptor fkDescriptor = associationMapping.getForeignKeyDescriptor(); final String lhsTable; final ModelPart lhsPart; - if ( associationMapping.getSide() == ForeignKeyDescriptor.Side.KEY ) { + if ( associationMapping.getSideNature() == ForeignKeyDescriptor.Nature.KEY ) { lhsTable = fkDescriptor.getKeyTable(); lhsPart = fkDescriptor.getKeyPart(); } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/DomainResultCreationState.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/DomainResultCreationState.java index 92a4f0e449..4017f3841b 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/DomainResultCreationState.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/DomainResultCreationState.java @@ -87,7 +87,7 @@ public interface DomainResultCreationState { * Returns the part of the foreign key that is currently being resolved, * or null if no foreign key is currently being resolved. */ - ForeignKeyDescriptor.Side getCurrentlyResolvingForeignKeyPart(); + ForeignKeyDescriptor.Nature getCurrentlyResolvingForeignKeyPart(); - void setCurrentlyResolvingForeignKeyPart(ForeignKeyDescriptor.Side currentlyResolvingForeignKeySide); + void setCurrentlyResolvingForeignKeyPart(ForeignKeyDescriptor.Nature currentlyResolvingForeignKeySide); } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularBiDirectionalFetchImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularBiDirectionalFetchImpl.java index 66ee61f193..2600935b18 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularBiDirectionalFetchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularBiDirectionalFetchImpl.java @@ -151,8 +151,8 @@ public class CircularBiDirectionalFetchImpl implements BiDirectionalFetch, Assoc } @Override - public ForeignKeyDescriptor.Side getSide() { - return ( (Association) fetchParent ).getSide(); + public ForeignKeyDescriptor.Nature getSideNature() { + return ( (Association) fetchParent ).getSideNature(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularFetchImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularFetchImpl.java index 5801688428..c9225350fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularFetchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularFetchImpl.java @@ -180,8 +180,8 @@ public class CircularFetchImpl implements BiDirectionalFetch, Association { } @Override - public ForeignKeyDescriptor.Side getSide() { - return ( (Association) fetchParent ).getSide(); + public ForeignKeyDescriptor.Nature getSideNature() { + return ( (Association) fetchParent ).getSideNature(); } @Override