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.from.TableReferenceCollector;
import org.hibernate.sql.ast.tree.predicate.Predicate; 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.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.DomainResultCreationState;
import org.hibernate.sql.results.spi.Fetch; import org.hibernate.sql.results.spi.Fetch;
import org.hibernate.sql.results.spi.FetchParent; import org.hibernate.sql.results.spi.FetchParent;
@ -120,13 +120,12 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
); );
} }
return new EntityFetchImpl( return new EntityFetch(
fetchParent, fetchParent,
this, this,
lockMode, lockMode,
!selected, !selected,
fetchablePath, fetchablePath,
foreignKeyDescriptor.createDomainResult( fetchablePath, lhsTableGroup, creationState ),
creationState creationState
); );
} }

View File

@ -9,13 +9,10 @@ package org.hibernate.sql.results.internal.domain.entity;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.metamodel.mapping.EntityValuedModelPart;
import org.hibernate.metamodel.mapping.internal.SingularAssociationAttributeMapping; import org.hibernate.metamodel.mapping.internal.SingularAssociationAttributeMapping;
import org.hibernate.query.NavigablePath; import org.hibernate.query.NavigablePath;
import org.hibernate.sql.results.spi.AssemblerCreationState; 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.DomainResultAssembler;
import org.hibernate.sql.results.spi.DomainResultCreationState;
import org.hibernate.sql.results.spi.EntityInitializer; import org.hibernate.sql.results.spi.EntityInitializer;
import org.hibernate.sql.results.spi.Fetch; import org.hibernate.sql.results.spi.Fetch;
import org.hibernate.sql.results.spi.FetchParent; import org.hibernate.sql.results.spi.FetchParent;
@ -25,40 +22,25 @@ import org.hibernate.sql.results.spi.Initializer;
/** /**
* @author Andrea Boriero * @author Andrea Boriero
* @author Steve Ebersole
*/ */
public class EntityFetchImpl implements Fetch { public abstract class AbstractEntityFecth implements Fetch {
private final FetchParent fetchParent; private final FetchParent fetchParent;
private final SingularAssociationAttributeMapping fetchedAttribute; private final SingularAssociationAttributeMapping fetchedAttribute;
private final LockMode lockMode;
private boolean nullable;
private final NavigablePath navigablePath; private final NavigablePath navigablePath;
private final DomainResult fkResult; private final boolean nullable;
private final EntityResultImpl entityResult; private final LockMode lockMode;
public EntityFetchImpl( public AbstractEntityFecth(
FetchParent fetchParent, FetchParent fetchParent,
SingularAssociationAttributeMapping fetchedAttribute, SingularAssociationAttributeMapping fetchedAttribute,
LockMode lockMode,
boolean nullable,
NavigablePath navigablePath, NavigablePath navigablePath,
DomainResult fkResult, boolean nullable,
DomainResultCreationState creationState) { LockMode lockMode) {
this.fetchParent = fetchParent; this.fetchParent = fetchParent;
this.fetchedAttribute = fetchedAttribute; this.fetchedAttribute = fetchedAttribute;
this.lockMode = lockMode;
this.nullable = nullable;
this.navigablePath = navigablePath; this.navigablePath = navigablePath;
this.fkResult = fkResult; this.nullable = nullable;
this.lockMode = lockMode;
entityResult = new EntityResultImpl(
navigablePath,
(EntityValuedModelPart) fetchedAttribute.getMappedTypeDescriptor(),
null,
creationState
);
} }
@Override @Override
@ -84,16 +66,21 @@ public class EntityFetchImpl implements Fetch {
@Override @Override
public DomainResultAssembler createAssembler( public DomainResultAssembler createAssembler(
FetchParentAccess parentAccess, Consumer<Initializer> collector, AssemblerCreationState creationState) { FetchParentAccess parentAccess, Consumer<Initializer> collector, AssemblerCreationState creationState) {
EntityInitializer entityInitializer = new EntityFetchInitializer( EntityInitializer entityInitializer = getEntityInitializer(
entityResult, parentAccess,
navigablePath,
lockMode,
entityResult.getIdentifierResult(),
entityResult.getDiscriminatorResult(),
entityResult.getVersionResult(),
collector, collector,
creationState 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.query.NavigablePath;
import org.hibernate.sql.results.spi.AssemblerCreationState; import org.hibernate.sql.results.spi.AssemblerCreationState;
import org.hibernate.sql.results.spi.DomainResult; 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.DomainResultCreationState;
import org.hibernate.sql.results.spi.EntityInitializer; 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.FetchParent;
import org.hibernate.sql.results.spi.FetchParentAccess; import org.hibernate.sql.results.spi.FetchParentAccess;
import org.hibernate.sql.results.spi.Fetchable;
import org.hibernate.sql.results.spi.Initializer; import org.hibernate.sql.results.spi.Initializer;
/** /**
* @author Andrea Boriero * @author Andrea Boriero
* @author Steve Ebersole * @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 DomainResult fkResult;
private final DomainResultCreationState creationState;
public DelayedEntityFetchImpl( public DelayedEntityFetchImpl(
FetchParent fetchParent, FetchParent fetchParent,
@ -45,50 +36,23 @@ public class DelayedEntityFetchImpl implements Fetch {
NavigablePath navigablePath, NavigablePath navigablePath,
DomainResult fkResult, DomainResult fkResult,
DomainResultCreationState creationState) { DomainResultCreationState creationState) {
this.fetchParent = fetchParent; super( fetchParent, fetchedAttribute, navigablePath, nullable, lockMode );
this.fetchedAttribute = fetchedAttribute;
this.lockMode = lockMode;
this.nullable = nullable;
this.navigablePath = navigablePath;
this.fkResult = fkResult; this.fkResult = fkResult;
this.creationState = creationState;
} }
@Override @Override
public FetchParent getFetchParent() { protected EntityInitializer getEntityInitializer(
return fetchParent;
}
@Override
public Fetchable getFetchedMapping() {
return fetchedAttribute;
}
@Override
public NavigablePath getNavigablePath() {
return navigablePath;
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public DomainResultAssembler createAssembler(
FetchParentAccess parentAccess, FetchParentAccess parentAccess,
Consumer<Initializer> collector, Consumer<Initializer> collector,
AssemblerCreationState creationState) { AssemblerCreationState creationState) {
EntityInitializer entityInitializer = new DelayedEntityFetchInitializer( final SingularAssociationAttributeMapping fetchedAttribute = (SingularAssociationAttributeMapping) getFetchedMapping();
return new DelayedEntityFetchInitializer(
parentAccess, parentAccess,
navigablePath, getNavigablePath(),
fetchedAttribute.getMappedFetchStrategy(), fetchedAttribute.getMappedFetchStrategy(),
lockMode, getLockMode(),
(EntityPersister) fetchedAttribute.getMappedTypeDescriptor(), (EntityPersister) fetchedAttribute.getMappedTypeDescriptor(),
fkResult.createResultAssembler( collector, creationState ) 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
);
}
}