HHH-13725 - Fix issue with DelayedEntityFetchImpl and FK not referring to PK

This commit is contained in:
Andrea Boriero 2019-11-20 13:27:51 +00:00 committed by Steve Ebersole
parent 343dd979ba
commit c8a1728bde
3 changed files with 18 additions and 6 deletions

View File

@ -34,6 +34,7 @@ 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.EntityFetch; import org.hibernate.sql.results.internal.domain.entity.EntityFetch;
import org.hibernate.sql.results.spi.DomainResult;
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;
@ -48,6 +49,7 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
private final boolean isNullable; private final boolean isNullable;
private ForeignKeyDescriptor foreignKeyDescriptor; private ForeignKeyDescriptor foreignKeyDescriptor;
private final String referencedPropertyName; private final String referencedPropertyName;
private final boolean referringPrimaryKey;
public SingularAssociationAttributeMapping( public SingularAssociationAttributeMapping(
String name, String name,
@ -70,6 +72,7 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
this.sqlAliasStem = SqlAliasStemHelper.INSTANCE.generateStemFromAttributeName( name ); this.sqlAliasStem = SqlAliasStemHelper.INSTANCE.generateStemFromAttributeName( name );
this.isNullable = value.isNullable(); this.isNullable = value.isNullable();
referencedPropertyName = value.getReferencedPropertyName(); referencedPropertyName = value.getReferencedPropertyName();
referringPrimaryKey =value.isReferenceToPrimaryKey();
} }
public void setForeignKeyDescriptor(ForeignKeyDescriptor foreignKeyDescriptor){ public void setForeignKeyDescriptor(ForeignKeyDescriptor foreignKeyDescriptor){
@ -142,13 +145,23 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
); );
} }
final DomainResult result;
if ( referringPrimaryKey ) {
result = foreignKeyDescriptor.createDomainResult( fetchablePath, lhsTableGroup, creationState );
}
else {
result = ( (EntityPersister) getDeclaringType() ).getIdentifierMapping()
.createDomainResult( fetchablePath, lhsTableGroup, null, creationState );
}
return new DelayedEntityFetchImpl( return new DelayedEntityFetchImpl(
fetchParent, fetchParent,
this, this,
lockMode, lockMode,
isNullable, isNullable,
fetchablePath, fetchablePath,
foreignKeyDescriptor.createDomainResult( fetchablePath, lhsTableGroup, creationState ), result,
creationState creationState
); );
} }

View File

@ -26,7 +26,7 @@ import org.hibernate.sql.results.spi.Initializer;
*/ */
public class DelayedEntityFetchImpl extends AbstractEntityFecth { public class DelayedEntityFetchImpl extends AbstractEntityFecth {
private DomainResult fkResult; private DomainResult result;
public DelayedEntityFetchImpl( public DelayedEntityFetchImpl(
FetchParent fetchParent, FetchParent fetchParent,
@ -34,10 +34,10 @@ public class DelayedEntityFetchImpl extends AbstractEntityFecth {
LockMode lockMode, LockMode lockMode,
boolean nullable, boolean nullable,
NavigablePath navigablePath, NavigablePath navigablePath,
DomainResult fkResult, DomainResult result,
DomainResultCreationState creationState) { DomainResultCreationState creationState) {
super( fetchParent, fetchedAttribute, navigablePath, nullable, lockMode ); super( fetchParent, fetchedAttribute, navigablePath, nullable, lockMode );
this.fkResult = fkResult; this.result = result;
} }
@Override @Override
@ -52,7 +52,7 @@ public class DelayedEntityFetchImpl extends AbstractEntityFecth {
fetchedAttribute.getMappedFetchStrategy(), fetchedAttribute.getMappedFetchStrategy(),
getLockMode(), getLockMode(),
(EntityPersister) fetchedAttribute.getMappedTypeDescriptor(), (EntityPersister) fetchedAttribute.getMappedTypeDescriptor(),
fkResult.createResultAssembler( collector, creationState ) result.createResultAssembler( collector, creationState )
); );
} }
} }

View File

@ -216,7 +216,6 @@ public class EntityWithBidirectionalOneToOneTest extends SessionFactoryBasedFunc
} }
@Test @Test
@FailureExpected
public void testHqlSelectParent() { public void testHqlSelectParent() {
inTransaction( inTransaction(
session -> { session -> {