Fix wrong key model of FK for inverse FK side

This commit is contained in:
Christian Beikov 2021-05-07 12:46:38 +02:00
parent 867dd52ab3
commit a864e25339
3 changed files with 30 additions and 10 deletions

View File

@ -61,6 +61,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor {
private AssociationKey associationKey; private AssociationKey associationKey;
public EmbeddedForeignKeyDescriptor( public EmbeddedForeignKeyDescriptor(
EmbeddableValuedModelPart keyMappingType,
EmbeddableValuedModelPart targetMappingType, EmbeddableValuedModelPart targetMappingType,
String keyTable, String keyTable,
SelectableMappings keySelectableMappings, SelectableMappings keySelectableMappings,
@ -72,11 +73,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor {
this.targetTable = targetTable; this.targetTable = targetTable;
this.targetSelectableMappings = targetSelectableMappings; this.targetSelectableMappings = targetSelectableMappings;
this.targetMappingType = targetMappingType; this.targetMappingType = targetMappingType;
this.keyMappingType = EmbeddedAttributeMapping.createInverseModelPart( this.keyMappingType = keyMappingType;
targetMappingType,
keySelectableMappings,
creationProcess
);
creationProcess.registerInitializationCallback( creationProcess.registerInitializationCallback(
"Embedded (composite) FK descriptor " + targetMappingType.getNavigableRole(), "Embedded (composite) FK descriptor " + targetMappingType.getNavigableRole(),

View File

@ -1134,6 +1134,11 @@ public class MappingModelCreationHelper {
if ( inverse ) { if ( inverse ) {
return new EmbeddedForeignKeyDescriptor( return new EmbeddedForeignKeyDescriptor(
embeddableValuedModelPart, embeddableValuedModelPart,
EmbeddedAttributeMapping.createInverseModelPart(
embeddableValuedModelPart,
keySelectableMappings,
creationProcess
),
embeddableValuedModelPart.getContainingTableExpression(), embeddableValuedModelPart.getContainingTableExpression(),
embeddableValuedModelPart.getEmbeddableTypeDescriptor(), embeddableValuedModelPart.getEmbeddableTypeDescriptor(),
keyTableExpression, keyTableExpression,
@ -1143,6 +1148,11 @@ public class MappingModelCreationHelper {
} }
else { else {
return new EmbeddedForeignKeyDescriptor( return new EmbeddedForeignKeyDescriptor(
EmbeddedAttributeMapping.createInverseModelPart(
embeddableValuedModelPart,
keySelectableMappings,
creationProcess
),
embeddableValuedModelPart, embeddableValuedModelPart,
keyTableExpression, keyTableExpression,
keySelectableMappings, keySelectableMappings,

View File

@ -610,9 +610,6 @@ public class ToOneAttributeMapping
); );
} }
//noinspection rawtypes
final DomainResult keyResult;
/* /*
1. No JoinTable 1. No JoinTable
Model: Model:
@ -637,15 +634,21 @@ public class ToOneAttributeMapping
*/ */
final boolean isKeyReferringSide;
if ( isFetchingForeignKey( fetchParent.getNavigablePath() ) ) {
isKeyReferringSide = !this.isKeyReferringSide;
}
else{
isKeyReferringSide = this.isKeyReferringSide;
}
final DomainResult<?> keyResult = foreignKeyDescriptor.createDomainResult( fetchablePath, parentTableGroup, isKeyReferringSide, creationState );
boolean selectByUniqueKey; boolean selectByUniqueKey;
if ( isKeyReferringSide ) { if ( isKeyReferringSide ) {
// case 1.2 // case 1.2
keyResult = foreignKeyDescriptor.createDomainResult( fetchablePath, parentTableGroup, creationState );
selectByUniqueKey = false; selectByUniqueKey = false;
} }
else { else {
// case 1.1 // case 1.1
keyResult = foreignKeyDescriptor.createDomainResult( fetchablePath, parentTableGroup, isKeyReferringSide, creationState );
selectByUniqueKey = bidirectionalAttributeName != null; selectByUniqueKey = bidirectionalAttributeName != null;
} }
@ -669,6 +672,16 @@ public class ToOneAttributeMapping
); );
} }
private boolean isFetchingForeignKey(NavigablePath p) {
while ( p != null ) {
if ( ForeignKeyDescriptor.PART_NAME.equals( p.getLocalName() ) ) {
return true;
}
p = p.getParent();
}
return false;
}
@Override @Override
public <T> DomainResult<T> createDelayedDomainResult( public <T> DomainResult<T> createDelayedDomainResult(
NavigablePath navigablePath, NavigablePath navigablePath,