Fix OneToOneFormulaTest#testOneToOneFormula()

This commit is contained in:
Andrea Boriero 2021-05-06 12:33:15 +02:00
parent d6a6c29bd4
commit 0157af79f7
5 changed files with 67 additions and 11 deletions

View File

@ -299,7 +299,7 @@ public class ToOneAttributeMapping
} }
public boolean canJoinForeignKey(EntityIdentifierMapping identifierMapping) { public boolean canJoinForeignKey(EntityIdentifierMapping identifierMapping) {
return isKeyReferringSide && identifierMapping == getForeignKeyDescriptor().getTargetPart(); return isKeyReferringSide && identifierMapping == getForeignKeyDescriptor().getTargetPart() && !isNullable;
} }
public String getReferencedPropertyName() { public String getReferencedPropertyName() {

View File

@ -57,7 +57,7 @@ public class LazyTableGroup extends AbstractColumnReferenceQualifier implements
return tableGroup; return tableGroup;
} }
private TableGroup getTableGroup() { public TableGroup getTableGroup() {
if ( tableGroup != null ) { if ( tableGroup != null ) {
return tableGroup; return tableGroup;
} }

View File

@ -129,6 +129,7 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
if ( existing.getNavigablePath().equals( navigablePath ) if ( existing.getNavigablePath().equals( navigablePath )
&& fetchedModelPart.getNavigableRole() && fetchedModelPart.getNavigableRole()
.equals( existing.getInitializedPart().getNavigableRole() ) ) { .equals( existing.getInitializedPart().getNavigableRole() ) ) {
assert fetchedModelPart == existing.getInitializedPart();
return existing; return existing;
} }
} }

View File

@ -19,6 +19,7 @@ import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
import org.hibernate.persister.entity.AbstractEntityPersister; import org.hibernate.persister.entity.AbstractEntityPersister;
import org.hibernate.query.EntityIdentifierNavigablePath; import org.hibernate.query.EntityIdentifierNavigablePath;
import org.hibernate.query.NavigablePath; import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.tree.from.LazyTableGroup;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.results.graph.AbstractFetchParent; import org.hibernate.sql.results.graph.AbstractFetchParent;
import org.hibernate.sql.results.graph.DomainResult; import org.hibernate.sql.results.graph.DomainResult;
@ -73,15 +74,27 @@ public abstract class AbstractEntityResultGraphNode extends AbstractFetchParent
identifierResult = null; identifierResult = null;
visitIdentifierMapping( identifierNavigablePath, creationState, identifierMapping, entityTableGroup ); visitIdentifierMapping( identifierNavigablePath, creationState, identifierMapping, entityTableGroup );
} }
else if ( referencedModelPart instanceof ToOneAttributeMapping else if ( referencedModelPart instanceof ToOneAttributeMapping ) {
&& ( (ToOneAttributeMapping) referencedModelPart ).canJoinForeignKey( identifierMapping ) ) {
// If we don't do this here, LazyTableGroup#getTableReferenceInternal would have to use the target table in case {id} is encountered // If we don't do this here, LazyTableGroup#getTableReferenceInternal would have to use the target table in case {id} is encountered
identifierResult = ( (ToOneAttributeMapping) referencedModelPart ).getForeignKeyDescriptor().createDomainResult( if ( ( (ToOneAttributeMapping) referencedModelPart ).canJoinForeignKey( identifierMapping ) ) {
navigablePath, identifierResult = ( (ToOneAttributeMapping) referencedModelPart ).getForeignKeyDescriptor()
creationState.getSqlAstCreationState().getFromClauseAccess().findTableGroup( navigablePath.getParent() ), .createDomainResult(
null, navigablePath,
creationState creationState.getSqlAstCreationState()
); .getFromClauseAccess()
.findTableGroup( navigablePath.getParent() ),
creationState
);
}
else {
identifierResult = identifierMapping.createDomainResult(
identifierNavigablePath,
( (LazyTableGroup) entityTableGroup ).getTableGroup(),
null,
creationState
);
}
} }
else { else {
identifierResult = identifierMapping.createDomainResult( identifierResult = identifierMapping.createDomainResult(
@ -144,7 +157,7 @@ public abstract class AbstractEntityResultGraphNode extends AbstractFetchParent
attributeMapping -> { attributeMapping -> {
if ( attributeMapping instanceof ToOneAttributeMapping ) { if ( attributeMapping instanceof ToOneAttributeMapping ) {
( (ToOneAttributeMapping) attributeMapping ).getForeignKeyDescriptor().createDomainResult( ( (ToOneAttributeMapping) attributeMapping ).getForeignKeyDescriptor().createDomainResult(
navigablePath, navigablePath.getParent(),
entityTableGroup, entityTableGroup,
null, null,
creationState creationState

View File

@ -64,6 +64,48 @@ public class EagerKeyManyToOneTest {
scope.inTransaction( scope.inTransaction(
session -> { session -> {
try { try {
/*
select
card0_.id as id1_0_0_,
card0_.field_card_id as field_ca2_0_0_,
card0_."field_key_id" as field_ke3_0_0_,
cardfield1_.card_id as card_id1_1_1_,
cardfield1_."key_id" as key_id2_1_1_,
card2_.id as id1_0_2_,
card2_.field_card_id as field_ca2_0_2_,
card2_."field_key_id" as field_ke3_0_2_,
key3_.id as id1_2_3_
from
Card card0_
left outer join
CardField cardfield1_
on card0_.field_card_id=cardfield1_.card_id
and card0_."field_key_id"=cardfield1_."key_id"
left outer join
Card card2_
on cardfield1_.card_id=card2_.id
left outer join
"key" key3_
on cardfield1_."key_id"=key3_.id
where
card0_.id=?
11:08:42,367 TRACE BasicBinder:64 - binding parameter [1] as [VARCHAR] - [cardId]
11:08:42,370 TRACE BasicExtractor:60 - extracted value ([id1_0_2_] : [VARCHAR]) - [cardId]
11:08:42,370 TRACE BasicExtractor:60 - extracted value ([id1_2_3_] : [VARCHAR]) - [keyId]
11:08:42,370 TRACE BasicExtractor:60 - extracted value ([card_id1_1_1_] : [VARCHAR]) - [cardId]
11:08:42,370 TRACE BasicExtractor:60 - extracted value ([key_id2_1_1_] : [VARCHAR]) - [keyId]
11:08:42,371 TRACE BasicExtractor:60 - extracted value ([field_ca2_0_2_] : [VARCHAR]) - [cardId]
11:08:42,372 TRACE BasicExtractor:60 - extracted value ([field_ke3_0_2_] : [VARCHAR]) - [keyId]
11:08:42,372 DEBUG SQL:144 -
select
key0_.id as id1_2_0_
from
"key" key0_
where
key0_.id=?
11:08:42,372 TRACE BasicBinder:64 - binding parameter [1] as [VARCHAR] - [keyId]
*/
Card card = session.get( Card.class, CARD_ID ); Card card = session.get( Card.class, CARD_ID );
CardField cf = card.getField(); CardField cf = card.getField();