diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SingularAssociationAttributeMapping.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SingularAssociationAttributeMapping.java index da27837870..612b8fc645 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SingularAssociationAttributeMapping.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SingularAssociationAttributeMapping.java @@ -30,7 +30,7 @@ import org.hibernate.sql.ast.tree.from.TableGroupJoinProducer; import org.hibernate.sql.ast.tree.from.TableReferenceCollector; import org.hibernate.sql.ast.tree.predicate.Predicate; import org.hibernate.sql.results.internal.domain.entity.DelayedEntityFetchImpl; -import org.hibernate.sql.results.internal.domain.entity.EntityFetchImpl; +import org.hibernate.sql.results.internal.domain.entity.EntityFetch; import org.hibernate.sql.results.spi.DomainResultCreationState; import org.hibernate.sql.results.spi.Fetch; import org.hibernate.sql.results.spi.FetchParent; @@ -120,13 +120,12 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu ); } - return new EntityFetchImpl( + return new EntityFetch( fetchParent, this, lockMode, !selected, fetchablePath, - foreignKeyDescriptor.createDomainResult( fetchablePath, lhsTableGroup, creationState ), creationState ); } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/EntityFetchImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/AbstractEntityFecth.java similarity index 66% rename from hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/EntityFetchImpl.java rename to hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/AbstractEntityFecth.java index 61d81aa0cb..7a37ad89ac 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/EntityFetchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/AbstractEntityFecth.java @@ -9,13 +9,10 @@ package org.hibernate.sql.results.internal.domain.entity; import java.util.function.Consumer; import org.hibernate.LockMode; -import org.hibernate.metamodel.mapping.EntityValuedModelPart; import org.hibernate.metamodel.mapping.internal.SingularAssociationAttributeMapping; import org.hibernate.query.NavigablePath; import org.hibernate.sql.results.spi.AssemblerCreationState; -import org.hibernate.sql.results.spi.DomainResult; import org.hibernate.sql.results.spi.DomainResultAssembler; -import org.hibernate.sql.results.spi.DomainResultCreationState; import org.hibernate.sql.results.spi.EntityInitializer; import org.hibernate.sql.results.spi.Fetch; import org.hibernate.sql.results.spi.FetchParent; @@ -25,40 +22,25 @@ import org.hibernate.sql.results.spi.Initializer; /** * @author Andrea Boriero - * @author Steve Ebersole */ -public class EntityFetchImpl implements Fetch { - +public abstract class AbstractEntityFecth implements Fetch { private final FetchParent fetchParent; private final SingularAssociationAttributeMapping fetchedAttribute; - private final LockMode lockMode; - private boolean nullable; private final NavigablePath navigablePath; - private final DomainResult fkResult; - private final EntityResultImpl entityResult; + private final boolean nullable; + private final LockMode lockMode; - public EntityFetchImpl( + public AbstractEntityFecth( FetchParent fetchParent, SingularAssociationAttributeMapping fetchedAttribute, - LockMode lockMode, - boolean nullable, NavigablePath navigablePath, - DomainResult fkResult, - DomainResultCreationState creationState) { + boolean nullable, + LockMode lockMode) { this.fetchParent = fetchParent; this.fetchedAttribute = fetchedAttribute; - this.lockMode = lockMode; - this.nullable = nullable; this.navigablePath = navigablePath; - this.fkResult = fkResult; - - entityResult = new EntityResultImpl( - navigablePath, - (EntityValuedModelPart) fetchedAttribute.getMappedTypeDescriptor(), - null, - creationState - ); - + this.nullable = nullable; + this.lockMode = lockMode; } @Override @@ -84,16 +66,21 @@ public class EntityFetchImpl implements Fetch { @Override public DomainResultAssembler createAssembler( FetchParentAccess parentAccess, Consumer collector, AssemblerCreationState creationState) { - EntityInitializer entityInitializer = new EntityFetchInitializer( - entityResult, - navigablePath, - lockMode, - entityResult.getIdentifierResult(), - entityResult.getDiscriminatorResult(), - entityResult.getVersionResult(), + EntityInitializer entityInitializer = getEntityInitializer( + parentAccess, collector, creationState ); - return new EntityAssembler( fetchedAttribute.getJavaTypeDescriptor(), entityInitializer ); + collector.accept( entityInitializer ); + return new EntityAssembler( getFetchedMapping().getJavaTypeDescriptor(), entityInitializer ); } + + public LockMode getLockMode() { + return lockMode; + } + + protected abstract EntityInitializer getEntityInitializer( + FetchParentAccess parentAccess, + Consumer collector, + AssemblerCreationState creationState); } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/DelayedEntityFetchImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/DelayedEntityFetchImpl.java index 734d2b4555..dda0920ca0 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/DelayedEntityFetchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/DelayedEntityFetchImpl.java @@ -14,28 +14,19 @@ import org.hibernate.persister.entity.EntityPersister; import org.hibernate.query.NavigablePath; import org.hibernate.sql.results.spi.AssemblerCreationState; import org.hibernate.sql.results.spi.DomainResult; -import org.hibernate.sql.results.spi.DomainResultAssembler; import org.hibernate.sql.results.spi.DomainResultCreationState; import org.hibernate.sql.results.spi.EntityInitializer; -import org.hibernate.sql.results.spi.Fetch; import org.hibernate.sql.results.spi.FetchParent; import org.hibernate.sql.results.spi.FetchParentAccess; -import org.hibernate.sql.results.spi.Fetchable; import org.hibernate.sql.results.spi.Initializer; /** * @author Andrea Boriero * @author Steve Ebersole */ -public class DelayedEntityFetchImpl implements Fetch { +public class DelayedEntityFetchImpl extends AbstractEntityFecth { - private FetchParent fetchParent; - private SingularAssociationAttributeMapping fetchedAttribute; - private final LockMode lockMode; - private final NavigablePath navigablePath; - private final boolean nullable; private DomainResult fkResult; - private final DomainResultCreationState creationState; public DelayedEntityFetchImpl( FetchParent fetchParent, @@ -45,50 +36,23 @@ public class DelayedEntityFetchImpl implements Fetch { NavigablePath navigablePath, DomainResult fkResult, DomainResultCreationState creationState) { - this.fetchParent = fetchParent; - this.fetchedAttribute = fetchedAttribute; - this.lockMode = lockMode; - this.nullable = nullable; - this.navigablePath = navigablePath; + super( fetchParent, fetchedAttribute, navigablePath, nullable, lockMode ); this.fkResult = fkResult; - this.creationState = creationState; - } @Override - public FetchParent getFetchParent() { - return fetchParent; - } - - @Override - public Fetchable getFetchedMapping() { - return fetchedAttribute; - } - - @Override - public NavigablePath getNavigablePath() { - return navigablePath; - } - - @Override - public boolean isNullable() { - return nullable; - } - - @Override - public DomainResultAssembler createAssembler( + protected EntityInitializer getEntityInitializer( FetchParentAccess parentAccess, Consumer collector, AssemblerCreationState creationState) { - EntityInitializer entityInitializer = new DelayedEntityFetchInitializer( + final SingularAssociationAttributeMapping fetchedAttribute = (SingularAssociationAttributeMapping) getFetchedMapping(); + return new DelayedEntityFetchInitializer( parentAccess, - navigablePath, + getNavigablePath(), fetchedAttribute.getMappedFetchStrategy(), - lockMode, + getLockMode(), (EntityPersister) fetchedAttribute.getMappedTypeDescriptor(), fkResult.createResultAssembler( collector, creationState ) ); - collector.accept( entityInitializer ); - return new EntityAssembler( fetchedAttribute.getJavaTypeDescriptor(), entityInitializer ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/EntityFetch.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/EntityFetch.java new file mode 100644 index 0000000000..0fbbdb2ca4 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/entity/EntityFetch.java @@ -0,0 +1,63 @@ +/* + * 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.sql.results.internal.domain.entity; + +import java.util.function.Consumer; + +import org.hibernate.LockMode; +import org.hibernate.metamodel.mapping.EntityValuedModelPart; +import org.hibernate.metamodel.mapping.internal.SingularAssociationAttributeMapping; +import org.hibernate.query.NavigablePath; +import org.hibernate.sql.results.spi.AssemblerCreationState; +import org.hibernate.sql.results.spi.DomainResultCreationState; +import org.hibernate.sql.results.spi.EntityInitializer; +import org.hibernate.sql.results.spi.FetchParent; +import org.hibernate.sql.results.spi.FetchParentAccess; +import org.hibernate.sql.results.spi.Initializer; + +/** + * @author Andrea Boriero + * @author Steve Ebersole + */ +public class EntityFetch extends AbstractEntityFecth { + + private final EntityResultImpl entityResult; + + public EntityFetch( + FetchParent fetchParent, + SingularAssociationAttributeMapping fetchedAttribute, + LockMode lockMode, + boolean nullable, + NavigablePath navigablePath, + DomainResultCreationState creationState) { + super( fetchParent, fetchedAttribute, navigablePath, nullable, lockMode ); + + entityResult = new EntityResultImpl( + navigablePath, + (EntityValuedModelPart) fetchedAttribute.getMappedTypeDescriptor(), + null, + creationState + ); + } + + @Override + protected EntityInitializer getEntityInitializer( + FetchParentAccess parentAccess, + Consumer collector, + AssemblerCreationState creationState) { + return new EntityFetchInitializer( + entityResult, + getNavigablePath(), + getLockMode(), + entityResult.getIdentifierResult(), + entityResult.getDiscriminatorResult(), + entityResult.getVersionResult(), + collector, + creationState + ); + } +}