HHH-15357 Fix access to mapped by map key

This commit is contained in:
Christian Beikov 2022-06-23 20:21:05 +02:00
parent 9cff075a89
commit 2b78f99ea8
2 changed files with 16 additions and 1 deletions

View File

@ -466,7 +466,7 @@ public class EntityCollectionPart
// to preserve the cardinality. Also, the OneToManyTableGroup has no reference to the parent table group
if ( !collectionDescriptor.isOneToMany() && targetKeyPropertyNames.contains( name ) ) {
if ( fkTargetModelPart instanceof ToOneAttributeMapping ) {
return fkTargetModelPart;
return ( (ToOneAttributeMapping) fkTargetModelPart ).findSubPart( name, targetType );
}
final ModelPart keyPart = fkDescriptor.getKeyPart();
if ( keyPart instanceof EmbeddableValuedModelPart && keyPart instanceof VirtualModelPart ) {

View File

@ -106,4 +106,19 @@ public class MapIssueTest {
}
);
}
@Test
@TestForIssue( jiraKey = "HHH-15357")
public void testSelectMapKeyFk(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(
s -> {
s.createQuery( "select key(c).id from MapOwner as o left join o.contents c" ).list();
statementInspector.assertExecutedCount( 1 );
// Assert that only the collection table and element table are joined
statementInspector.assertNumberOfJoins( 0, 2 );
}
);
}
}