diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/AbstractCollectionInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/AbstractCollectionInitializer.java index 22aebffb54..2b0315c3eb 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/AbstractCollectionInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/AbstractCollectionInitializer.java @@ -93,6 +93,7 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial if ( collectionKey != null ) { final SharedSessionContractImplementor session = rowProcessingState.getSession(); final PersistenceContext persistenceContext = session.getPersistenceContext(); + final FetchParentAccess fetchParentAccess = parentAccess.findFirstEntityDescriptorAccess(); final LoadingCollectionEntry loadingEntry = persistenceContext.getLoadContexts() .findLoadingCollectionEntry( collectionKey ); @@ -100,7 +101,7 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial if ( loadingEntry != null ) { collectionInstance = loadingEntry.getCollectionInstance(); if ( collectionInstance.getOwner() == null ) { - parentAccess.registerResolutionListener( + fetchParentAccess.registerResolutionListener( owner -> collectionInstance.setOwner( owner ) ); } @@ -112,7 +113,7 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial if ( existing != null ) { collectionInstance = existing; if ( collectionInstance.getOwner() == null ) { - parentAccess.registerResolutionListener( + fetchParentAccess.registerResolutionListener( owner -> collectionInstance.setOwner( owner ) ); } @@ -129,7 +130,7 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial session ); - parentAccess.registerResolutionListener( + fetchParentAccess.registerResolutionListener( owner -> collectionInstance.setOwner( owner ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/embedded/DeleteOneToManyOrphansEmbeddedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/embedded/OneToManyInEmbeddedTest.java similarity index 77% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/embedded/DeleteOneToManyOrphansEmbeddedTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/embedded/OneToManyInEmbeddedTest.java index 16d6176996..0c756e4138 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/embedded/DeleteOneToManyOrphansEmbeddedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/embedded/OneToManyInEmbeddedTest.java @@ -22,20 +22,24 @@ import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; import jakarta.persistence.OneToMany; +import static org.junit.jupiter.api.Assertions.assertTrue; + /** * @author Marco Belladelli */ -@JiraKey("HHH-15864") @Jpa(annotatedClasses = { - DeleteOneToManyOrphansEmbeddedTest.ChildEntity.class, DeleteOneToManyOrphansEmbeddedTest.ParentEntity.class + OneToManyInEmbeddedTest.ChildEntity.class, + OneToManyInEmbeddedTest.ParentEntity.class }) -public class DeleteOneToManyOrphansEmbeddedTest { +@JiraKey("HHH-15864") +public class OneToManyInEmbeddedTest { @BeforeEach public void setUp(EntityManagerFactoryScope scope) { scope.inTransaction( entityManager -> { - ParentEntity parentEntity = new ParentEntity( 1, new ChildEntityWrapper( List.of( new ChildEntity() ) ) ); + ParentEntity parentEntity = new ParentEntity( new ChildEntityWrapper( List.of( new ChildEntity() ) ) ); entityManager.persist( parentEntity ); } ); } @@ -49,11 +53,17 @@ public class DeleteOneToManyOrphansEmbeddedTest { } @Test - public void testOrphanRemoval(EntityManagerFactoryScope scope) { + public void testOrphanRemovalInEmbedded(EntityManagerFactoryScope scope) { scope.inTransaction( entityManager -> { ParentEntity parentEntity = entityManager.find( ParentEntity.class, 1 ); + parentEntity.getChildEntityWrapper().getChildEntities().clear(); entityManager.remove( parentEntity ); } ); + + scope.inTransaction( entityManager -> assertTrue( + entityManager.createQuery( "from ChildEntity" ).getResultList().isEmpty(), + "Orphan entity was not removed" + ) ); } @Entity(name = "ChildEntity") @@ -74,7 +84,7 @@ public class DeleteOneToManyOrphansEmbeddedTest { @Embeddable public static class ChildEntityWrapper { @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) - // @JoinColumn(name = "parent_entity_id", referencedColumnName = "id") + @JoinColumn(name = "parent_entity_id", referencedColumnName = "id") private List childEntities; public ChildEntityWrapper() { @@ -96,7 +106,7 @@ public class DeleteOneToManyOrphansEmbeddedTest { @Entity(name = "ParentEntity") public static class ParentEntity { @Id - // @GeneratedValue(strategy = GenerationType.IDENTITY) + @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Embedded @@ -105,8 +115,7 @@ public class DeleteOneToManyOrphansEmbeddedTest { public ParentEntity() { } - public ParentEntity(int id, ChildEntityWrapper childEntityWrapper) { - this.id = id; + public ParentEntity(ChildEntityWrapper childEntityWrapper) { this.childEntityWrapper = childEntityWrapper; }