HHH-13725 - Fix issue circular fetch detection

This commit is contained in:
Andrea Boriero 2019-11-20 15:08:47 +00:00 committed by Steve Ebersole
parent c8a1728bde
commit a53772f6a0
3 changed files with 34 additions and 39 deletions

View File

@ -335,7 +335,9 @@ public class MetamodelSelectBuilderProcess {
}
try {
fetchDepth++;
if(!(fetchable instanceof BasicValuedModelPart)) {
fetchDepth--;
}
Fetch fetch = fetchable.generateFetch(
fetchParent,
fetchablePath,
@ -348,7 +350,9 @@ public class MetamodelSelectBuilderProcess {
fetches.add( fetch );
}
finally {
fetchDepth--;
if(!(fetchable instanceof BasicValuedModelPart)) {
fetchDepth--;
}
}
};

View File

@ -14,6 +14,7 @@ import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.EntityValuedModelPart;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.ManagedMappingType;
import org.hibernate.metamodel.mapping.ModelPartContainer;
import org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.property.access.spi.PropertyAccess;
@ -38,7 +39,6 @@ import org.hibernate.sql.results.spi.DomainResult;
import org.hibernate.sql.results.spi.DomainResultCreationState;
import org.hibernate.sql.results.spi.Fetch;
import org.hibernate.sql.results.spi.FetchParent;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/**
* @author Steve Ebersole
@ -48,6 +48,7 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
private final String sqlAliasStem;
private final boolean isNullable;
private ForeignKeyDescriptor foreignKeyDescriptor;
private final String referencedPropertyName;
private final boolean referringPrimaryKey;
@ -72,17 +73,21 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
this.sqlAliasStem = SqlAliasStemHelper.INSTANCE.generateStemFromAttributeName( name );
this.isNullable = value.isNullable();
referencedPropertyName = value.getReferencedPropertyName();
referringPrimaryKey =value.isReferenceToPrimaryKey();
referringPrimaryKey = value.isReferenceToPrimaryKey();
}
public void setForeignKeyDescriptor(ForeignKeyDescriptor foreignKeyDescriptor){
public void setForeignKeyDescriptor(ForeignKeyDescriptor foreignKeyDescriptor) {
this.foreignKeyDescriptor = foreignKeyDescriptor;
}
public ForeignKeyDescriptor getForeignKeyDescriptor(){
public ForeignKeyDescriptor getForeignKeyDescriptor() {
return this.foreignKeyDescriptor;
}
public String getReferencedPropertyName() {
return referencedPropertyName;
}
@Override
public EntityMappingType getMappedTypeDescriptor() {
return (EntityMappingType) super.getMappedTypeDescriptor();
@ -252,41 +257,27 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
if ( parentParentNavigablePath == null ) {
return false;
}
if ( getAttributeName().equals( parentParentNavigablePath.getLocalName() ) ) {
return true;
}
else {
final String entityName = getMappedTypeDescriptor().getEntityName();
if ( panentNaviblePath.getLocalName().equals( referencedPropertyName ) ) {
if ( parentParentNavigablePath.getLocalName().equals( entityName ) ) {
return true;
}
else {
// need to check if panentNaviblePath.getParent()
// FetchParent fetchParent1 = fetchParentByNavigableFullPath.get( parentParentNavigablePath.getFullPath() );
final JavaTypeDescriptor resultJavaTypeDescriptor = fetchParent.getResultJavaTypeDescriptor();
if ( getEntityMappingType().getJavaTypeDescriptor().getJavaType()
.equals( resultJavaTypeDescriptor.getJavaType() ) ) {
return true;
}
return false;
}
final ModelPartContainer modelPart = creationState.getSqlAstCreationState()
.getFromClauseAccess()
.findTableGroup( parentParentNavigablePath )
.getModelPart();
final SingularAssociationAttributeMapping part = (SingularAssociationAttributeMapping) modelPart
.findSubPart( panentNaviblePath.getLocalName(), null );
if ( panentNaviblePath.getLocalName().equals( referencedPropertyName )
&& part.getFetchableName().equals( referencedPropertyName ) ) {
return true;
}
else {
if ( parentParentNavigablePath.getLocalName().equals( entityName ) ) {
return true;
}
JavaTypeDescriptor parentParentJavaTypeDescriptor = creationState.getSqlAstCreationState()
.getFromClauseAccess()
.findTableGroup( parentParentNavigablePath )
.getModelPart()
.getJavaTypeDescriptor();
if ( getEntityMappingType().getJavaTypeDescriptor().getJavaType()
.equals( parentParentJavaTypeDescriptor.getJavaType() ) ) {
return true;
}
return false;
else if ( part.getReferencedPropertyName() != null
&& part.getReferencedPropertyName().equals( getAttributeName() ) ) {
return true;
}
}
return false;
}
}

View File

@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -134,7 +135,6 @@ public class EntityWithBidirectionalOneToOneTest extends SessionFactoryBasedFunc
}
@Test
@FailureExpected
public void testGetParent3() {
inTransaction( session -> {
@ -211,7 +211,7 @@ public class EntityWithBidirectionalOneToOneTest extends SessionFactoryBasedFunc
"The child2 eager OneToOne association is not initialized"
);
assertThat( child2.getParent1(), notNullValue() );
assertThat( child2.getParent2(), notNullValue() );
assertThat( child2.getParent2(), nullValue() );
} );
}