HHH-16033 Many-to-Many inverse mapping referencing the same class uses pk instead of fk field for removal

This commit is contained in:
Andrea Boriero 2023-01-12 18:12:21 +01:00 committed by Christian Beikov
parent ea8b0649a2
commit a4e2fe57cc
7 changed files with 56 additions and 0 deletions

View File

@ -263,6 +263,7 @@ public class DefaultDeleteEventListener implements DeleteEventListener, Callback
&& !persister.hasSubclasses() //TODO: should be unnecessary, using EntityPersister.getSubclassPropertyTypeClosure(), etc
&& !persister.hasCascadeDelete()
&& !persister.hasNaturalIdentifier()
&& !persister.hasCollectionNotReferencingPK()
&& !hasRegisteredRemoveCallbacks( persister )
&& !hasCustomEventListeners( source );
}

View File

@ -8,6 +8,7 @@ package org.hibernate.mapping;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
@ -36,6 +37,7 @@ import org.hibernate.jpa.event.spi.CallbackDefinition;
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.Alias;
import org.hibernate.type.CollectionType;
import org.hibernate.type.Type;
import org.hibernate.type.spi.TypeConfiguration;
@ -1131,6 +1133,28 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl
this.hasSubselectLoadableCollections = hasSubselectCollections;
}
public boolean hasCollectionNotReferencingPK() {
return hasCollectionNotReferencingPK( properties );
}
private boolean hasCollectionNotReferencingPK(Collection<Property> properties) {
for ( Property property : properties ) {
final Value value = property.getValue();
if ( value instanceof Component ) {
if ( hasCollectionNotReferencingPK( ( (Component) value ).getProperties() ) ) {
return true;
}
}
else if ( value instanceof org.hibernate.mapping.Collection ) {
final org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection) value;
if ( !( (CollectionType) collection.getType() ).useLHSPrimaryKey() ) {
return true;
}
}
}
return false;
}
public boolean hasPartitionedSelectionMapping() {
if ( getSuperclass() != null && getSuperclass().hasPartitionedSelectionMapping() ) {
return true;

View File

@ -355,6 +355,7 @@ public abstract class AbstractEntityPersister
protected final int batchSize;
private final boolean hasSubselectLoadableCollections;
private final boolean hasPartitionedSelectionMapping;
private final boolean hasCollectionNotReferencingPK;
protected final String rowIdName;
// The optional SQL string defined in the where attribute
@ -533,6 +534,7 @@ public abstract class AbstractEntityPersister
batchSize = batch;
hasSubselectLoadableCollections = persistentClass.hasSubselectLoadableCollections();
hasPartitionedSelectionMapping = persistentClass.hasPartitionedSelectionMapping();
hasCollectionNotReferencingPK = persistentClass.hasCollectionNotReferencingPK();
propertyMapping = new BasicEntityPropertyMapping( this );
@ -4435,6 +4437,11 @@ public abstract class AbstractEntityPersister
return hasSubselectLoadableCollections;
}
@Override
public boolean hasCollectionNotReferencingPK() {
return hasCollectionNotReferencingPK;
}
@Override
public int[] getNaturalIdentifierProperties() {
return entityMetamodel.getNaturalIdentifierProperties();

View File

@ -282,6 +282,15 @@ public interface EntityPersister extends EntityMappingType, RootTableGroupProduc
*/
boolean hasSubselectLoadableCollections();
/**
* Determine whether this entity contains references to persistent collections
* not referencing the primary key.
*
* @return True if the entity contains a collection not referencing the primary key; false otherwise.
* @since 6.2
*/
boolean hasCollectionNotReferencingPK();
/**
* Determine whether this entity has any
* (non-{@linkplain org.hibernate.engine.spi.CascadeStyles#NONE none}) cascading.

View File

@ -271,6 +271,11 @@ public class GoofyPersisterClassProvider implements PersisterClassResolver {
return false;
}
@Override
public boolean hasCollectionNotReferencingPK() {
return false;
}
@Override
public boolean hasCascades() {
return false;

View File

@ -298,6 +298,11 @@ public class PersisterClassProviderTest {
return false;
}
@Override
public boolean hasCollectionNotReferencingPK() {
return false;
}
@Override
public boolean hasCascades() {
return false;

View File

@ -696,6 +696,11 @@ public class CustomPersister implements EntityPersister {
return false;
}
@Override
public boolean hasCollectionNotReferencingPK() {
return false;
}
@Override
public int[] getNaturalIdentifierProperties() {
return null;