Support FK column access optimization also for associations in embeddables of an element collection

This commit is contained in:
Christian Beikov 2022-01-31 10:34:03 +01:00
parent afdedb0fc5
commit 1e823386f6
1 changed files with 26 additions and 14 deletions

View File

@ -359,20 +359,7 @@ public class ToOneAttributeMapping
isConstrained = bootValue.isConstrained(); isConstrained = bootValue.isConstrained();
this.navigableRole = navigableRole; this.navigableRole = navigableRole;
final CollectionPart.Nature nature = CollectionPart.Nature.fromNameExact( this.declaringTableGroupProducer = resolveDeclaringTableGroupProducer( declaringEntityPersister );
getNavigableRole().getParent().getLocalName()
);
if ( nature == null ) {
// This is a simple to-one association
this.declaringTableGroupProducer = declaringEntityPersister;
}
else {
// This is a collection part i.e. to-many association
final String collectionRoleName = getNavigableRole().getParent().getParent().getLocalName();
this.declaringTableGroupProducer = ( (PluralAttributeMapping) declaringEntityPersister.findAttributeMapping(
collectionRoleName.substring( collectionRoleName.lastIndexOf( '.' ) + 1 )
) );
}
if ( referencedPropertyName == null ) { if ( referencedPropertyName == null ) {
final Set<String> targetKeyPropertyNames = new HashSet<>( 2 ); final Set<String> targetKeyPropertyNames = new HashSet<>( 2 );
targetKeyPropertyNames.add( EntityIdentifierMapping.ROLE_LOCAL_NAME ); targetKeyPropertyNames.add( EntityIdentifierMapping.ROLE_LOCAL_NAME );
@ -471,6 +458,31 @@ public class ToOneAttributeMapping
} }
} }
private TableGroupProducer resolveDeclaringTableGroupProducer(EntityPersister declaringEntityPersister) {
// Also handle cases where a collection contains an embeddable, that contains an association
NavigableRole parentRole = getNavigableRole().getParent();
String collectionRole = null;
do {
final CollectionPart.Nature nature = CollectionPart.Nature.fromNameExact(
parentRole.getLocalName()
);
if (nature != null) {
collectionRole = parentRole.getParent().getFullPath();
break;
}
parentRole = parentRole.getParent();
} while (parentRole != null);
if ( collectionRole != null ) {
// This is a collection part i.e. to-many association
return declaringEntityPersister.getFactory().getMappingMetamodel()
.findCollectionDescriptor( collectionRole )
.getAttributeMapping();
}
// This is a simple to-one association
return declaringEntityPersister;
}
private ToOneAttributeMapping( private ToOneAttributeMapping(
ToOneAttributeMapping original, ToOneAttributeMapping original,
ManagedMappingType declaringType, ManagedMappingType declaringType,