From 84523cd0d994f0577a5da2b0c74418863e0411b0 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 26 Jan 2021 15:24:54 +0100 Subject: [PATCH] fix issue with EmbeddableFKDescriptor --- .../internal/MappingModelCreationHelper.java | 71 +++---------------- .../internal/PluralAttributeMappingImpl.java | 2 +- .../hibernate/orm/test/bidi/Auction.hbm.xml | 2 +- ...iLevelCascadeCollectionEmbeddableTest.java | 3 - 4 files changed, 12 insertions(+), 66 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java index cf5415d58a..be2e344f77 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java @@ -927,7 +927,7 @@ public class MappingModelCreationHelper { } else if ( fkTarget instanceof EmbeddableValuedModelPart ) { final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = - buildTargetingEmbeddableForeignKeyDescriptor( + buildEmbeddableForeignKeyDescriptor( (EmbeddableValuedModelPart) fkTarget, bootValueMapping, dialect, @@ -947,7 +947,6 @@ public class MappingModelCreationHelper { ToOneAttributeMapping attributeMapping, Property bootProperty, ToOne bootValueMapping, - EntityMappingType declaringEntityDescriptor, Dialect dialect, MappingModelCreationProcess creationProcess) { if ( attributeMapping.getForeignKeyDescriptor() != null ) { @@ -960,10 +959,13 @@ public class MappingModelCreationHelper { final EntityPersister referencedEntityDescriptor = creationProcess .getEntityPersister( bootValueMapping.getReferencedEntityName() ); - String referencedPropertyName = bootValueMapping.getReferencedPropertyName(); - if ( referencedPropertyName == null && bootValueMapping instanceof OneToOne ) { + final String referencedPropertyName; + if ( bootValueMapping instanceof OneToOne ) { referencedPropertyName = ( (OneToOne) bootValueMapping ).getMappedByProperty(); } + else { + referencedPropertyName = bootValueMapping.getReferencedPropertyName(); + } if ( referencedPropertyName != null ) { final ModelPart modelPart = referencedEntityDescriptor.findSubPart( referencedPropertyName ); @@ -978,7 +980,7 @@ public class MappingModelCreationHelper { ); } else if ( modelPart instanceof EmbeddableValuedModelPart ) { - final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = buildForeignKeyReferringEmbeddableDescriptor( + final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = buildEmbeddableForeignKeyDescriptor( (EmbeddableValuedModelPart) modelPart, bootValueMapping, dialect, @@ -995,13 +997,7 @@ public class MappingModelCreationHelper { return; } - final ModelPart fkTarget; - if ( bootValueMapping.isReferenceToPrimaryKey() ) { - fkTarget = referencedEntityDescriptor.getIdentifierMapping(); - } - else { - fkTarget = declaringEntityDescriptor.getIdentifierMapping(); - } + final ModelPart fkTarget = referencedEntityDescriptor.getIdentifierMapping(); if ( fkTarget instanceof BasicValuedModelPart ) { final BasicValuedModelPart simpleFkTarget = (BasicValuedModelPart) fkTarget; @@ -1036,7 +1032,7 @@ public class MappingModelCreationHelper { attributeMapping.setForeignKeyDescriptor( foreignKeyDescriptor ); } else if ( fkTarget instanceof EmbeddableValuedModelPart ) { - final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = buildTargetingEmbeddableForeignKeyDescriptor( + final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = buildEmbeddableForeignKeyDescriptor( (EmbeddableValuedModelPart) fkTarget, bootValueMapping, dialect, @@ -1052,52 +1048,7 @@ public class MappingModelCreationHelper { } } - public static EmbeddedForeignKeyDescriptor buildForeignKeyReferringEmbeddableDescriptor( - EmbeddableValuedModelPart embeddableValuedModelPart, - Value bootValueMapping, - Dialect dialect, - MappingModelCreationProcess creationProcess) { - final SelectionMappings targetSelectionMappings; - final String targetTableExpression; - if ( bootValueMapping instanceof Collection ) { - final Collection collectionBootValueMapping = (Collection) bootValueMapping; - targetTableExpression = getTableIdentifierExpression( - collectionBootValueMapping.getCollectionTable(), - creationProcess - ); - targetSelectionMappings = SelectionMappingsImpl.from( - targetTableExpression, - collectionBootValueMapping.getKey(), - creationProcess.getCreationContext().getSessionFactory(), - dialect, - creationProcess.getSqmFunctionRegistry() - ); - } - else { - targetTableExpression = getTableIdentifierExpression( - bootValueMapping.getTable(), - creationProcess - ); - targetSelectionMappings = SelectionMappingsImpl.from( - targetTableExpression, - bootValueMapping, - creationProcess.getCreationContext().getSessionFactory(), - dialect, - creationProcess.getSqmFunctionRegistry() - ); - } - return new EmbeddedForeignKeyDescriptor( - embeddableValuedModelPart, - embeddableValuedModelPart.getContainingTableExpression(), - embeddableValuedModelPart.getEmbeddableTypeDescriptor(), - targetTableExpression, - targetSelectionMappings, - creationProcess - ); - } - - - public static EmbeddedForeignKeyDescriptor buildTargetingEmbeddableForeignKeyDescriptor( + public static EmbeddedForeignKeyDescriptor buildEmbeddableForeignKeyDescriptor( EmbeddableValuedModelPart embeddableValuedModelPart, Value bootValueMapping, Dialect dialect, @@ -1159,7 +1110,6 @@ public class MappingModelCreationHelper { referencedAttributeMapping, property, (ToOne) property.getValue(), - referencedEntityDescriptor, dialect, creationProcess ); @@ -1475,7 +1425,6 @@ public class MappingModelCreationHelper { attributeMapping, bootProperty, (ToOne) bootProperty.getValue(), - declaringType.findContainingEntityMapping(), dialect, creationProcess ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/PluralAttributeMappingImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/PluralAttributeMappingImpl.java index a8eb03cfa7..2ae5d1fe09 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/PluralAttributeMappingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/PluralAttributeMappingImpl.java @@ -342,7 +342,7 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping ); } else if ( fkTargetPart instanceof EmbeddableValuedModelPart ) { - return MappingModelCreationHelper.buildTargetingEmbeddableForeignKeyDescriptor( + return MappingModelCreationHelper.buildEmbeddableForeignKeyDescriptor( (EmbeddableValuedModelPart) fkTargetPart, fkBootDescriptorSource, dialect, diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bidi/Auction.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/bidi/Auction.hbm.xml index 5d4c32bd42..13b474ab79 100755 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bidi/Auction.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bidi/Auction.hbm.xml @@ -23,7 +23,7 @@ - id true diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/cascade/multilevel/MultiLevelCascadeCollectionEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/cascade/multilevel/MultiLevelCascadeCollectionEmbeddableTest.java index 503e7f9afc..db84004cb5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/cascade/multilevel/MultiLevelCascadeCollectionEmbeddableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/cascade/multilevel/MultiLevelCascadeCollectionEmbeddableTest.java @@ -33,8 +33,6 @@ import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.NotImplementedYet; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; -import org.jboss.logging.Logger; - import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -50,7 +48,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; @NotImplementedYet(reason = "NotImplementedYetException thrown in the initialization method, by NativeNonSelectQueryPlanImpl.executeUpdate()") public class MultiLevelCascadeCollectionEmbeddableTest { - private static final Logger log = Logger.getLogger( MultiLevelCascadeCollectionEmbeddableTest.class ); private boolean initialized = false; //TODO this could be implemented with @BeforeAll, if we move to Junit 5.5 or higher. The way to intercept exceptions in this method, inside