HHH-13725 - Implement ManyToOne associations support done some code refactoring

This commit is contained in:
Andrea Boriero 2019-11-14 18:53:50 +00:00 committed by Steve Ebersole
parent d2851888f9
commit 0094236d98
4 changed files with 93 additions and 80 deletions

View File

@ -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
);
}

View File

@ -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<Initializer> 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<Initializer> collector,
AssemblerCreationState creationState);
}

View File

@ -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<Initializer> 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 );
}
}

View File

@ -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<Initializer> collector,
AssemblerCreationState creationState) {
return new EntityFetchInitializer(
entityResult,
getNavigablePath(),
getLockMode(),
entityResult.getIdentifierResult(),
entityResult.getDiscriminatorResult(),
entityResult.getVersionResult(),
collector,
creationState
);
}
}