Skip to content

Commit a4e2fe5

Browse files
dreab8beikov
authored andcommitted
HHH-16033 Many-to-Many inverse mapping referencing the same class uses pk instead of fk field for removal
1 parent ea8b064 commit a4e2fe5

File tree

7 files changed

+56
-0
lines changed

7 files changed

+56
-0
lines changed

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ private boolean canBeDeletedWithoutLoading(EventSource source, EntityPersister p
263263
&& !persister.hasSubclasses() //TODO: should be unnecessary, using EntityPersister.getSubclassPropertyTypeClosure(), etc
264264
&& !persister.hasCascadeDelete()
265265
&& !persister.hasNaturalIdentifier()
266+
&& !persister.hasCollectionNotReferencingPK()
266267
&& !hasRegisteredRemoveCallbacks( persister )
267268
&& !hasCustomEventListeners( source );
268269
}

hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.io.Serializable;
1010
import java.util.ArrayList;
11+
import java.util.Collection;
1112
import java.util.Collections;
1213
import java.util.HashSet;
1314
import java.util.Iterator;
@@ -36,6 +37,7 @@
3637
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
3738
import org.hibernate.service.ServiceRegistry;
3839
import org.hibernate.sql.Alias;
40+
import org.hibernate.type.CollectionType;
3941
import org.hibernate.type.Type;
4042
import org.hibernate.type.spi.TypeConfiguration;
4143

@@ -1131,6 +1133,28 @@ public void setSubselectLoadableCollections(boolean hasSubselectCollections) {
11311133
this.hasSubselectLoadableCollections = hasSubselectCollections;
11321134
}
11331135

1136+
public boolean hasCollectionNotReferencingPK() {
1137+
return hasCollectionNotReferencingPK( properties );
1138+
}
1139+
1140+
private boolean hasCollectionNotReferencingPK(Collection<Property> properties) {
1141+
for ( Property property : properties ) {
1142+
final Value value = property.getValue();
1143+
if ( value instanceof Component ) {
1144+
if ( hasCollectionNotReferencingPK( ( (Component) value ).getProperties() ) ) {
1145+
return true;
1146+
}
1147+
}
1148+
else if ( value instanceof org.hibernate.mapping.Collection ) {
1149+
final org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection) value;
1150+
if ( !( (CollectionType) collection.getType() ).useLHSPrimaryKey() ) {
1151+
return true;
1152+
}
1153+
}
1154+
}
1155+
return false;
1156+
}
1157+
11341158
public boolean hasPartitionedSelectionMapping() {
11351159
if ( getSuperclass() != null && getSuperclass().hasPartitionedSelectionMapping() ) {
11361160
return true;

hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ public abstract class AbstractEntityPersister
355355
protected final int batchSize;
356356
private final boolean hasSubselectLoadableCollections;
357357
private final boolean hasPartitionedSelectionMapping;
358+
private final boolean hasCollectionNotReferencingPK;
358359
protected final String rowIdName;
359360

360361
// The optional SQL string defined in the where attribute
@@ -533,6 +534,7 @@ public AbstractEntityPersister(
533534
batchSize = batch;
534535
hasSubselectLoadableCollections = persistentClass.hasSubselectLoadableCollections();
535536
hasPartitionedSelectionMapping = persistentClass.hasPartitionedSelectionMapping();
537+
hasCollectionNotReferencingPK = persistentClass.hasCollectionNotReferencingPK();
536538

537539
propertyMapping = new BasicEntityPropertyMapping( this );
538540

@@ -4435,6 +4437,11 @@ public boolean hasSubselectLoadableCollections() {
44354437
return hasSubselectLoadableCollections;
44364438
}
44374439

4440+
@Override
4441+
public boolean hasCollectionNotReferencingPK() {
4442+
return hasCollectionNotReferencingPK;
4443+
}
4444+
44384445
@Override
44394446
public int[] getNaturalIdentifierProperties() {
44404447
return entityMetamodel.getNaturalIdentifierProperties();

hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ default void visitQuerySpaces(Consumer<String> querySpaceConsumer) {
282282
*/
283283
boolean hasSubselectLoadableCollections();
284284

285+
/**
286+
* Determine whether this entity contains references to persistent collections
287+
* not referencing the primary key.
288+
*
289+
* @return True if the entity contains a collection not referencing the primary key; false otherwise.
290+
* @since 6.2
291+
*/
292+
boolean hasCollectionNotReferencingPK();
293+
285294
/**
286295
* Determine whether this entity has any
287296
* (non-{@linkplain org.hibernate.engine.spi.CascadeStyles#NONE none}) cascading.

hibernate-core/src/test/java/org/hibernate/orm/test/cfg/persister/GoofyPersisterClassProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ public boolean hasSubselectLoadableCollections() {
271271
return false;
272272
}
273273

274+
@Override
275+
public boolean hasCollectionNotReferencingPK() {
276+
return false;
277+
}
278+
274279
@Override
275280
public boolean hasCascades() {
276281
return false;

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/PersisterClassProviderTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ public boolean hasSubselectLoadableCollections() {
298298
return false;
299299
}
300300

301+
@Override
302+
public boolean hasCollectionNotReferencingPK() {
303+
return false;
304+
}
305+
301306
@Override
302307
public boolean hasCascades() {
303308
return false;

hibernate-core/src/test/java/org/hibernate/orm/test/legacy/CustomPersister.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,11 @@ public boolean hasSubselectLoadableCollections() {
696696
return false;
697697
}
698698

699+
@Override
700+
public boolean hasCollectionNotReferencingPK() {
701+
return false;
702+
}
703+
699704
@Override
700705
public int[] getNaturalIdentifierProperties() {
701706
return null;

0 commit comments

Comments
 (0)