HHH-15864 Fix collection's owner referring to Embeddable class

This commit is contained in:
Marco Belladelli 2022-12-22 12:58:08 +01:00 committed by Christian Beikov
parent 84b0da4970
commit 724ae7986d
2 changed files with 22 additions and 12 deletions

View File

@ -93,6 +93,7 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial
if ( collectionKey != null ) { if ( collectionKey != null ) {
final SharedSessionContractImplementor session = rowProcessingState.getSession(); final SharedSessionContractImplementor session = rowProcessingState.getSession();
final PersistenceContext persistenceContext = session.getPersistenceContext(); final PersistenceContext persistenceContext = session.getPersistenceContext();
final FetchParentAccess fetchParentAccess = parentAccess.findFirstEntityDescriptorAccess();
final LoadingCollectionEntry loadingEntry = persistenceContext.getLoadContexts() final LoadingCollectionEntry loadingEntry = persistenceContext.getLoadContexts()
.findLoadingCollectionEntry( collectionKey ); .findLoadingCollectionEntry( collectionKey );
@ -100,7 +101,7 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial
if ( loadingEntry != null ) { if ( loadingEntry != null ) {
collectionInstance = loadingEntry.getCollectionInstance(); collectionInstance = loadingEntry.getCollectionInstance();
if ( collectionInstance.getOwner() == null ) { if ( collectionInstance.getOwner() == null ) {
parentAccess.registerResolutionListener( fetchParentAccess.registerResolutionListener(
owner -> collectionInstance.setOwner( owner ) owner -> collectionInstance.setOwner( owner )
); );
} }
@ -112,7 +113,7 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial
if ( existing != null ) { if ( existing != null ) {
collectionInstance = existing; collectionInstance = existing;
if ( collectionInstance.getOwner() == null ) { if ( collectionInstance.getOwner() == null ) {
parentAccess.registerResolutionListener( fetchParentAccess.registerResolutionListener(
owner -> collectionInstance.setOwner( owner ) owner -> collectionInstance.setOwner( owner )
); );
} }
@ -129,7 +130,7 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial
session session
); );
parentAccess.registerResolutionListener( fetchParentAccess.registerResolutionListener(
owner -> collectionInstance.setOwner( owner ) owner -> collectionInstance.setOwner( owner )
); );

View File

@ -22,20 +22,24 @@ import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany; import jakarta.persistence.OneToMany;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* @author Marco Belladelli * @author Marco Belladelli
*/ */
@JiraKey("HHH-15864")
@Jpa(annotatedClasses = { @Jpa(annotatedClasses = {
DeleteOneToManyOrphansEmbeddedTest.ChildEntity.class, DeleteOneToManyOrphansEmbeddedTest.ParentEntity.class OneToManyInEmbeddedTest.ChildEntity.class,
OneToManyInEmbeddedTest.ParentEntity.class
}) })
public class DeleteOneToManyOrphansEmbeddedTest { @JiraKey("HHH-15864")
public class OneToManyInEmbeddedTest {
@BeforeEach @BeforeEach
public void setUp(EntityManagerFactoryScope scope) { public void setUp(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> { 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 ); entityManager.persist( parentEntity );
} ); } );
} }
@ -49,11 +53,17 @@ public class DeleteOneToManyOrphansEmbeddedTest {
} }
@Test @Test
public void testOrphanRemoval(EntityManagerFactoryScope scope) { public void testOrphanRemovalInEmbedded(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> { scope.inTransaction( entityManager -> {
ParentEntity parentEntity = entityManager.find( ParentEntity.class, 1 ); ParentEntity parentEntity = entityManager.find( ParentEntity.class, 1 );
parentEntity.getChildEntityWrapper().getChildEntities().clear();
entityManager.remove( parentEntity ); entityManager.remove( parentEntity );
} ); } );
scope.inTransaction( entityManager -> assertTrue(
entityManager.createQuery( "from ChildEntity" ).getResultList().isEmpty(),
"Orphan entity was not removed"
) );
} }
@Entity(name = "ChildEntity") @Entity(name = "ChildEntity")
@ -74,7 +84,7 @@ public class DeleteOneToManyOrphansEmbeddedTest {
@Embeddable @Embeddable
public static class ChildEntityWrapper { public static class ChildEntityWrapper {
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
// @JoinColumn(name = "parent_entity_id", referencedColumnName = "id") @JoinColumn(name = "parent_entity_id", referencedColumnName = "id")
private List<ChildEntity> childEntities; private List<ChildEntity> childEntities;
public ChildEntityWrapper() { public ChildEntityWrapper() {
@ -96,7 +106,7 @@ public class DeleteOneToManyOrphansEmbeddedTest {
@Entity(name = "ParentEntity") @Entity(name = "ParentEntity")
public static class ParentEntity { public static class ParentEntity {
@Id @Id
// @GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; private int id;
@Embedded @Embedded
@ -105,8 +115,7 @@ public class DeleteOneToManyOrphansEmbeddedTest {
public ParentEntity() { public ParentEntity() {
} }
public ParentEntity(int id, ChildEntityWrapper childEntityWrapper) { public ParentEntity(ChildEntityWrapper childEntityWrapper) {
this.id = id;
this.childEntityWrapper = childEntityWrapper; this.childEntityWrapper = childEntityWrapper;
} }