|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.jpa.orphan.onetoone; |
| 6 | + |
| 7 | +import jakarta.persistence.Embeddable; |
| 8 | +import jakarta.persistence.Embedded; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.FetchType; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import jakarta.persistence.OneToOne; |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.Jira; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.junit.jupiter.api.AfterAll; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +@SessionFactory |
| 24 | +@DomainModel(annotatedClasses = { |
| 25 | + OneToOneLazyOrphanRemovalEmbeddedTest.OwnerEntity.class, |
| 26 | + OneToOneLazyOrphanRemovalEmbeddedTest.ToOneContainer.class, |
| 27 | + OneToOneLazyOrphanRemovalEmbeddedTest.ChildEntity.class |
| 28 | +}) |
| 29 | +@Jira("https://hibernate.atlassian.net/browse/HHH-9663") |
| 30 | +public class OneToOneLazyOrphanRemovalEmbeddedTest { |
| 31 | + @Test |
| 32 | + public void test(SessionFactoryScope scope) { |
| 33 | + // set association to null, should trigger orphan removal |
| 34 | + scope.inTransaction( session -> { |
| 35 | + final OwnerEntity owner = session.find( OwnerEntity.class, 1 ); |
| 36 | + final ToOneContainer container = owner.getContainer(); |
| 37 | + assertThat( container.getChild() ).isNotNull(); |
| 38 | + container.setChild( null ); |
| 39 | + session.merge( owner ); |
| 40 | + |
| 41 | + assertThat( session.find( OwnerEntity.class, 1 ).getContainer() ).isNotNull(); |
| 42 | + } ); |
| 43 | + |
| 44 | + // check orphan removal removed the entity correctly |
| 45 | + scope.inTransaction( session -> { |
| 46 | + final OwnerEntity owner = session.find( OwnerEntity.class, 1 ); |
| 47 | + final ToOneContainer container = owner.getContainer(); |
| 48 | + assertThat( container.getChild() ).isNull(); |
| 49 | + assertThat( session.find( ChildEntity.class, 1 ) ).isNull(); |
| 50 | + } ); |
| 51 | + } |
| 52 | + |
| 53 | + @BeforeAll |
| 54 | + public void setUp(SessionFactoryScope scope) { |
| 55 | + scope.inTransaction( session -> { |
| 56 | + final ChildEntity child = new ChildEntity( 1L, "child_entity" ); |
| 57 | + final ToOneContainer container = new ToOneContainer( child, 1 ); |
| 58 | + final OwnerEntity raceDriver = new OwnerEntity( 1L, container ); |
| 59 | + session.persist( child ); |
| 60 | + session.persist( raceDriver ); |
| 61 | + } ); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterAll |
| 65 | + public void tearDown(SessionFactoryScope scope) { |
| 66 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 67 | + } |
| 68 | + |
| 69 | + @Entity(name = "OwnerEntity") |
| 70 | + static class OwnerEntity { |
| 71 | + @Id |
| 72 | + private Long id; |
| 73 | + |
| 74 | + @Embedded |
| 75 | + private ToOneContainer container; |
| 76 | + |
| 77 | + public OwnerEntity() { |
| 78 | + } |
| 79 | + |
| 80 | + public OwnerEntity(Long id, ToOneContainer container) { |
| 81 | + this.id = id; |
| 82 | + this.container = container; |
| 83 | + } |
| 84 | + |
| 85 | + public ToOneContainer getContainer() { |
| 86 | + return container; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Embeddable |
| 91 | + static class ToOneContainer { |
| 92 | + @OneToOne(orphanRemoval = true, fetch = FetchType.LAZY) |
| 93 | + private ChildEntity child; |
| 94 | + |
| 95 | + private Integer count; |
| 96 | + |
| 97 | + public ToOneContainer() { |
| 98 | + } |
| 99 | + |
| 100 | + public ToOneContainer(ChildEntity child, Integer count) { |
| 101 | + this.child = child; |
| 102 | + this.count = count; |
| 103 | + } |
| 104 | + |
| 105 | + public ChildEntity getChild() { |
| 106 | + return child; |
| 107 | + } |
| 108 | + |
| 109 | + public void setChild(ChildEntity child) { |
| 110 | + this.child = child; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Entity(name = "Engine") |
| 115 | + public static class ChildEntity { |
| 116 | + @Id |
| 117 | + private Long id; |
| 118 | + |
| 119 | + private String name; |
| 120 | + |
| 121 | + public ChildEntity() { |
| 122 | + } |
| 123 | + |
| 124 | + public ChildEntity(Long id, String name) { |
| 125 | + this.id = id; |
| 126 | + this.name = name; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments