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.results.internal.domain.entity.DelayedEntityFetchImpl;
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.Fetch;
import org.hibernate.sql.results.spi.FetchParent;
@ -48,6 +49,7 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
private final boolean isNullable;
private ForeignKeyDescriptor foreignKeyDescriptor;
private final String referencedPropertyName;
private final boolean referringPrimaryKey;
public SingularAssociationAttributeMapping(
String name,
@ -70,6 +72,7 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
this.sqlAliasStem = SqlAliasStemHelper.INSTANCE.generateStemFromAttributeName( name );
this.isNullable = value.isNullable();
referencedPropertyName = value.getReferencedPropertyName();
referringPrimaryKey =value.isReferenceToPrimaryKey();
}
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(
fetchParent,
this,
lockMode,
isNullable,
fetchablePath,
foreignKeyDescriptor.createDomainResult( fetchablePath, lhsTableGroup, creationState ),
result,
creationState
);
}

View File

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

View File

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