From 2d0aad36b8087968ef0cf345aaa5de55cb16f47c Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 16 Jun 2020 20:26:25 +0100 Subject: [PATCH] Fix PluralAttribute manyToMany FK target part determination --- .../internal/MappingModelCreationHelper.java | 25 ++++++++++++++----- .../internal/PluralAttributeMappingImpl.java | 14 ++++++++--- .../domain/internal/BagAttributeImpl.java | 7 ------ .../domain/internal/MapAttributeImpl.java | 6 ----- 4 files changed, 30 insertions(+), 22 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 64a60dcb00..13cd6b5e10 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 @@ -983,20 +983,33 @@ public class MappingModelCreationHelper { Value bootValueMapping, Dialect dialect, MappingModelCreationProcess creationProcess) { - final List keyColumnExpressions = new ArrayList<>(bootValueMapping.getColumnSpan()); - bootValueMapping.getColumnIterator().forEachRemaining( - column -> - keyColumnExpressions.add( column.getText( dialect ) ) ); - final List mappedColumnExpressions = fkTarget.getMappedColumnExpressions(); final List targetColumnExpressions = new ArrayList<>( mappedColumnExpressions.size() ); mappedColumnExpressions.forEach( column -> targetColumnExpressions.add( column ) ); + final List keyColumnExpressions; + Table keyTableExpression; + if ( bootValueMapping instanceof Collection ) { + final Collection collectioBootValueMapping = (Collection) bootValueMapping; + keyTableExpression = collectioBootValueMapping.getCollectionTable(); + final KeyValue key = collectioBootValueMapping.getKey(); + keyColumnExpressions = new ArrayList<>( key.getColumnSpan() ); + key.getColumnIterator().forEachRemaining( + column -> + keyColumnExpressions.add( column.getText( dialect ) ) ); + } + else { + keyTableExpression = bootValueMapping.getTable(); + keyColumnExpressions = new ArrayList<>( bootValueMapping.getColumnSpan() ); + bootValueMapping.getColumnIterator().forEachRemaining( + column -> + keyColumnExpressions.add( column.getText( dialect ) ) ); + } return new EmbeddedForeignKeyDescriptor( (EmbeddedIdentifierMappingImpl) fkTarget, - getTableIdentifierExpression( bootValueMapping.getTable(), creationProcess ), + getTableIdentifierExpression( keyTableExpression, creationProcess ), keyColumnExpressions, fkTarget.getContainingTableExpression(), targetColumnExpressions, 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 b7af77d66b..720efb89a9 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 @@ -40,7 +40,9 @@ import org.hibernate.metamodel.mapping.ordering.OrderByFragment; import org.hibernate.metamodel.mapping.ordering.OrderByFragmentTranslator; import org.hibernate.metamodel.mapping.ordering.TranslationContext; import org.hibernate.metamodel.model.domain.NavigableRole; +import org.hibernate.persister.collection.AbstractCollectionPersister; import org.hibernate.persister.collection.CollectionPersister; +import org.hibernate.persister.entity.AbstractEntityPersister; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.Joinable; import org.hibernate.property.access.spi.PropertyAccess; @@ -234,9 +236,15 @@ public class PluralAttributeMappingImpl extends AbstractAttributeMapping impleme if ( collectionDescriptor.getElementType() instanceof EntityType ) { final EntityType elementEntityType = (EntityType) collectionDescriptor.getElementType(); associatedEntityDescriptor = creationProcess.getEntityPersister( elementEntityType.getAssociatedEntityName() ); - fkTargetPart = elementEntityType.isReferenceToPrimaryKey() - ? associatedEntityDescriptor.getIdentifierMapping() - : associatedEntityDescriptor.findSubPart( elementEntityType.getRHSUniqueKeyPropertyName() ); + if ( ( (AbstractEntityPersister) associatedEntityDescriptor ).getTableName() + .equals( ( (AbstractCollectionPersister) collectionDescriptor ).getTableName() ) ) { + fkTargetPart = creationProcess + .getEntityPersister( bootDescriptor.getOwner().getEntityName() ) + .getIdentifierMapping(); + } + else { + fkTargetPart = associatedEntityDescriptor.getIdentifierMapping(); + } fkBootDescriptorSource = bootDescriptor.getElement(); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/BagAttributeImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/BagAttributeImpl.java index caf274bdb1..1201d3203e 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/BagAttributeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/BagAttributeImpl.java @@ -8,10 +8,8 @@ package org.hibernate.metamodel.model.domain.internal; import java.util.Collection; -import org.hibernate.NotYetImplementedFor6Exception; import org.hibernate.metamodel.internal.MetadataContext; import org.hibernate.metamodel.model.domain.BagPersistentAttribute; -import org.hibernate.query.sqm.SqmPathSource; import org.hibernate.query.hql.spi.SqmCreationState; import org.hibernate.query.sqm.tree.SqmJoinType; import org.hibernate.query.sqm.tree.domain.SqmBagJoin; @@ -34,11 +32,6 @@ class BagAttributeImpl return CollectionType.COLLECTION; } - @Override - public SqmPathSource findSubPathSource(String name) { - throw new NotYetImplementedFor6Exception(); - } - @Override public SqmAttributeJoin createSqmJoin( SqmFrom lhs, diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MapAttributeImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MapAttributeImpl.java index 29e4c0b38a..ad394676d7 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MapAttributeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MapAttributeImpl.java @@ -8,7 +8,6 @@ package org.hibernate.metamodel.model.domain.internal; import java.util.Map; -import org.hibernate.NotYetImplementedFor6Exception; import org.hibernate.metamodel.internal.MetadataContext; import org.hibernate.metamodel.model.domain.MapPersistentAttribute; import org.hibernate.metamodel.model.domain.SimpleDomainType; @@ -66,11 +65,6 @@ class MapAttributeImpl extends AbstractPluralAttribute, V> return getKeyType(); } - @Override - public SqmPathSource findSubPathSource(String name) { - throw new NotYetImplementedFor6Exception(); - } - @Override public SqmAttributeJoin createSqmJoin( SqmFrom lhs, SqmJoinType joinType, String alias, boolean fetched, SqmCreationState creationState) {