|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.mapping.manytoone; |
| 8 | + |
| 9 | +import java.io.Serializable; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Set; |
| 12 | + |
| 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 jakarta.persistence.Embeddable; |
| 22 | +import jakarta.persistence.EmbeddedId; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.ManyToOne; |
| 26 | +import jakarta.persistence.OneToMany; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Staffan Hörke |
| 32 | + * @author Marco Belladelli |
| 33 | + */ |
| 34 | +@SessionFactory |
| 35 | +@DomainModel( annotatedClasses = { |
| 36 | + ManyToOneEmbeddedIdSelfReferenceJoinFetchTest.OrganizationId.class, |
| 37 | + ManyToOneEmbeddedIdSelfReferenceJoinFetchTest.OrganizationEmbedded.class, |
| 38 | + ManyToOneEmbeddedIdSelfReferenceJoinFetchTest.Organization.class |
| 39 | +} ) |
| 40 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-16473" ) |
| 41 | +public class ManyToOneEmbeddedIdSelfReferenceJoinFetchTest { |
| 42 | + @BeforeAll |
| 43 | + public void setup(SessionFactoryScope scope) { |
| 44 | + scope.inTransaction( session -> { |
| 45 | + final OrganizationEmbedded parent = new OrganizationEmbedded( |
| 46 | + new OrganizationId( 1L ), |
| 47 | + "parent_organization" |
| 48 | + ); |
| 49 | + final OrganizationEmbedded child = new OrganizationEmbedded( |
| 50 | + new OrganizationId( 2L ), |
| 51 | + "child_organization" |
| 52 | + ); |
| 53 | + parent.getChildren().add( child ); |
| 54 | + child.setParent( parent ); |
| 55 | + session.persist( parent ); |
| 56 | + session.persist( child ); |
| 57 | + } ); |
| 58 | + scope.inTransaction( session -> { |
| 59 | + final Organization parent = new Organization( 1L, "parent_organization" ); |
| 60 | + final Organization child = new Organization( 2L, "child_organization" ); |
| 61 | + parent.getChildren().add( child ); |
| 62 | + child.setParent( parent ); |
| 63 | + session.persist( parent ); |
| 64 | + session.persist( child ); |
| 65 | + } ); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterAll |
| 69 | + public void tearDown(SessionFactoryScope scope) { |
| 70 | + scope.inTransaction( session -> { |
| 71 | + session.createQuery( "from OrganizationEmbedded", OrganizationEmbedded.class ) |
| 72 | + .getResultList() |
| 73 | + .forEach( o -> o.setParent( null ) ); |
| 74 | + session.createQuery( "from Organization", Organization.class ) |
| 75 | + .getResultList() |
| 76 | + .forEach( o -> o.setParent( null ) ); |
| 77 | + } ); |
| 78 | + scope.inTransaction( session -> { |
| 79 | + session.createMutationQuery( "delete from OrganizationEmbedded" ).executeUpdate(); |
| 80 | + session.createMutationQuery( "delete from Organization" ).executeUpdate(); |
| 81 | + } ); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testChildWithEmbeddedId(SessionFactoryScope scope) { |
| 86 | + scope.inTransaction( session -> { |
| 87 | + final OrganizationEmbedded child = session.createQuery( |
| 88 | + "select child from OrganizationEmbedded child join fetch child.parent WHERE child.id = :id", |
| 89 | + OrganizationEmbedded.class |
| 90 | + ).setParameter( "id", new OrganizationId( 2L ) ).getSingleResult(); |
| 91 | + assertThat( child.getName() ).isEqualTo( "child_organization" ); |
| 92 | + assertThat( child.getParent() ).isNotNull(); |
| 93 | + assertThat( child.getParent().getName() ).isEqualTo( "parent_organization" ); |
| 94 | + } ); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testChildWithPrimitiveId(SessionFactoryScope scope) { |
| 99 | + scope.inTransaction( session -> { |
| 100 | + final Organization child = session.createQuery( |
| 101 | + "select child from Organization child join fetch child.parent where child.id = :id", |
| 102 | + Organization.class |
| 103 | + ).setParameter( "id", 2L ).getSingleResult(); |
| 104 | + assertThat( child.getName() ).isEqualTo( "child_organization" ); |
| 105 | + assertThat( child.getParent() ).isNotNull(); |
| 106 | + assertThat( child.getParent().getName() ).isEqualTo( "parent_organization" ); |
| 107 | + } ); |
| 108 | + } |
| 109 | + |
| 110 | + @Embeddable |
| 111 | + public static class OrganizationId implements Serializable { |
| 112 | + private Long id; |
| 113 | + |
| 114 | + public OrganizationId() { |
| 115 | + } |
| 116 | + |
| 117 | + public OrganizationId(Long id) { |
| 118 | + this.id = id; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Entity( name = "OrganizationEmbedded" ) |
| 123 | + public static class OrganizationEmbedded { |
| 124 | + @EmbeddedId |
| 125 | + private OrganizationId id; |
| 126 | + private String name; |
| 127 | + @ManyToOne |
| 128 | + private OrganizationEmbedded parent; |
| 129 | + @OneToMany( mappedBy = "parent" ) |
| 130 | + private Set<OrganizationEmbedded> children; |
| 131 | + |
| 132 | + public OrganizationEmbedded() { |
| 133 | + } |
| 134 | + |
| 135 | + public OrganizationEmbedded(OrganizationId id, String name) { |
| 136 | + this.id = id; |
| 137 | + this.name = name; |
| 138 | + this.children = new HashSet<>(); |
| 139 | + } |
| 140 | + |
| 141 | + public String getName() { |
| 142 | + return name; |
| 143 | + } |
| 144 | + |
| 145 | + public OrganizationEmbedded getParent() { |
| 146 | + return parent; |
| 147 | + } |
| 148 | + |
| 149 | + public void setParent(OrganizationEmbedded parent) { |
| 150 | + this.parent = parent; |
| 151 | + } |
| 152 | + |
| 153 | + public Set<OrganizationEmbedded> getChildren() { |
| 154 | + return children; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Entity( name = "Organization" ) |
| 159 | + public static class Organization { |
| 160 | + @Id |
| 161 | + private Long id; |
| 162 | + private String name; |
| 163 | + @ManyToOne |
| 164 | + private Organization parent; |
| 165 | + @OneToMany( mappedBy = "parent" ) |
| 166 | + private Set<Organization> children; |
| 167 | + |
| 168 | + public Organization() { |
| 169 | + } |
| 170 | + |
| 171 | + public Organization(Long id, String name) { |
| 172 | + this.id = id; |
| 173 | + this.name = name; |
| 174 | + this.children = new HashSet<>(); |
| 175 | + } |
| 176 | + |
| 177 | + public String getName() { |
| 178 | + return name; |
| 179 | + } |
| 180 | + |
| 181 | + public Organization getParent() { |
| 182 | + return parent; |
| 183 | + } |
| 184 | + |
| 185 | + public void setParent(Organization parent) { |
| 186 | + this.parent = parent; |
| 187 | + } |
| 188 | + |
| 189 | + public Set<Organization> getChildren() { |
| 190 | + return children; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments