HHH-16473 Add test for issue

This commit is contained in:
Staffan Hörke 2023-04-14 11:25:31 +02:00 committed by Marco Belladelli
parent 411cbb0a1b
commit aac301f979
No known key found for this signature in database
GPG Key ID: D1D0C3030AE3AA35
1 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,193 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.manytoone;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Staffan Hörke
* @author Marco Belladelli
*/
@SessionFactory
@DomainModel( annotatedClasses = {
ManyToOneEmbeddedIdSelfReferenceJoinFetchTest.OrganizationId.class,
ManyToOneEmbeddedIdSelfReferenceJoinFetchTest.OrganizationEmbedded.class,
ManyToOneEmbeddedIdSelfReferenceJoinFetchTest.Organization.class
} )
@Jira( "https://hibernate.atlassian.net/browse/HHH-16473" )
public class ManyToOneEmbeddedIdSelfReferenceJoinFetchTest {
@BeforeAll
public void setup(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final OrganizationEmbedded parent = new OrganizationEmbedded(
new OrganizationId( 1L ),
"parent_organization"
);
final OrganizationEmbedded child = new OrganizationEmbedded(
new OrganizationId( 2L ),
"child_organization"
);
parent.getChildren().add( child );
child.setParent( parent );
session.persist( parent );
session.persist( child );
} );
scope.inTransaction( session -> {
final Organization parent = new Organization( 1L, "parent_organization" );
final Organization child = new Organization( 2L, "child_organization" );
parent.getChildren().add( child );
child.setParent( parent );
session.persist( parent );
session.persist( child );
} );
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.createQuery( "from OrganizationEmbedded", OrganizationEmbedded.class )
.getResultList()
.forEach( o -> o.setParent( null ) );
session.createQuery( "from Organization", Organization.class )
.getResultList()
.forEach( o -> o.setParent( null ) );
} );
scope.inTransaction( session -> {
session.createMutationQuery( "delete from OrganizationEmbedded" ).executeUpdate();
session.createMutationQuery( "delete from Organization" ).executeUpdate();
} );
}
@Test
public void testChildWithEmbeddedId(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final OrganizationEmbedded child = session.createQuery(
"select child from OrganizationEmbedded child join fetch child.parent WHERE child.id = :id",
OrganizationEmbedded.class
).setParameter( "id", new OrganizationId( 2L ) ).getSingleResult();
assertThat( child.getName() ).isEqualTo( "child_organization" );
assertThat( child.getParent() ).isNotNull();
assertThat( child.getParent().getName() ).isEqualTo( "parent_organization" );
} );
}
@Test
public void testChildWithPrimitiveId(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final Organization child = session.createQuery(
"select child from Organization child join fetch child.parent where child.id = :id",
Organization.class
).setParameter( "id", 2L ).getSingleResult();
assertThat( child.getName() ).isEqualTo( "child_organization" );
assertThat( child.getParent() ).isNotNull();
assertThat( child.getParent().getName() ).isEqualTo( "parent_organization" );
} );
}
@Embeddable
public static class OrganizationId implements Serializable {
private Long id;
public OrganizationId() {
}
public OrganizationId(Long id) {
this.id = id;
}
}
@Entity( name = "OrganizationEmbedded" )
public static class OrganizationEmbedded {
@EmbeddedId
private OrganizationId id;
private String name;
@ManyToOne
private OrganizationEmbedded parent;
@OneToMany( mappedBy = "parent" )
private Set<OrganizationEmbedded> children;
public OrganizationEmbedded() {
}
public OrganizationEmbedded(OrganizationId id, String name) {
this.id = id;
this.name = name;
this.children = new HashSet<>();
}
public String getName() {
return name;
}
public OrganizationEmbedded getParent() {
return parent;
}
public void setParent(OrganizationEmbedded parent) {
this.parent = parent;
}
public Set<OrganizationEmbedded> getChildren() {
return children;
}
}
@Entity( name = "Organization" )
public static class Organization {
@Id
private Long id;
private String name;
@ManyToOne
private Organization parent;
@OneToMany( mappedBy = "parent" )
private Set<Organization> children;
public Organization() {
}
public Organization(Long id, String name) {
this.id = id;
this.name = name;
this.children = new HashSet<>();
}
public String getName() {
return name;
}
public Organization getParent() {
return parent;
}
public void setParent(Organization parent) {
this.parent = parent;
}
public Set<Organization> getChildren() {
return children;
}
}
}