fix issue with EmbeddableFKDescriptor

This commit is contained in:
Andrea Boriero 2021-01-26 15:24:54 +01:00
parent e97aa81916
commit 84523cd0d9
4 changed files with 12 additions and 66 deletions

View File

@ -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
);

View File

@ -342,7 +342,7 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping
);
}
else if ( fkTargetPart instanceof EmbeddableValuedModelPart ) {
return MappingModelCreationHelper.buildTargetingEmbeddableForeignKeyDescriptor(
return MappingModelCreationHelper.buildEmbeddableForeignKeyDescriptor(
(EmbeddableValuedModelPart) fkTargetPart,
fkBootDescriptorSource,
dialect,

View File

@ -23,7 +23,7 @@
<key column="auctionId"/>
<one-to-many class="Bid"/>
</bag>
<one-to-one name="successfulBid"
<one-to-one name="successfulBid"
property-ref="abc">
<formula>id</formula>
<formula>true</formula>

View File

@ -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