Skip to content

Commit

Permalink
HHH-15864 Fix collection's owner referring to Embeddable class
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel authored and beikov committed Dec 23, 2022
1 parent 9c91910 commit 595fbc5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Expand Up @@ -93,14 +93,15 @@ protected void resolveInstance(RowProcessingState rowProcessingState, boolean is
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 );

if ( loadingEntry != null ) {
collectionInstance = loadingEntry.getCollectionInstance();
if ( collectionInstance.getOwner() == null ) {
parentAccess.registerResolutionListener(
fetchParentAccess.registerResolutionListener(
owner -> collectionInstance.setOwner( owner )
);
}
Expand All @@ -112,7 +113,7 @@ protected void resolveInstance(RowProcessingState rowProcessingState, boolean is
if ( existing != null ) {
collectionInstance = existing;
if ( collectionInstance.getOwner() == null ) {
parentAccess.registerResolutionListener(
fetchParentAccess.registerResolutionListener(
owner -> collectionInstance.setOwner( owner )
);
}
Expand All @@ -129,7 +130,7 @@ protected void resolveInstance(RowProcessingState rowProcessingState, boolean is
session
);

parentAccess.registerResolutionListener(
fetchParentAccess.registerResolutionListener(
owner -> collectionInstance.setOwner( owner )
);

Expand Down
Expand Up @@ -22,20 +22,24 @@
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 );
} );
}
Expand All @@ -49,11 +53,17 @@ public void tearDown(EntityManagerFactoryScope scope) {
}

@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")
Expand All @@ -74,7 +84,7 @@ public void setId(int id) {
@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<ChildEntity> childEntities;

public ChildEntityWrapper() {
Expand All @@ -96,7 +106,7 @@ public void setChildEntities(List<ChildEntity> childEntities) {
@Entity(name = "ParentEntity")
public static class ParentEntity {
@Id
// @GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Embedded
Expand All @@ -105,8 +115,7 @@ public static class ParentEntity {
public ParentEntity() {
}

public ParentEntity(int id, ChildEntityWrapper childEntityWrapper) {
this.id = id;
public ParentEntity(ChildEntityWrapper childEntityWrapper) {
this.childEntityWrapper = childEntityWrapper;
}

Expand Down

0 comments on commit 595fbc5

Please sign in to comment.