HHH-17727 Add test for issue

This commit is contained in:
Marco Belladelli 2024-02-12 11:49:20 +01:00 committed by Christian Beikov
parent 778e5b1214
commit dfb56c2f36
2 changed files with 236 additions and 0 deletions

View File

@ -16,6 +16,7 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
@ -34,6 +35,9 @@ import static org.assertj.core.api.Assertions.assertThat;
CaseStatementWithTypeTest.JoinedParent.class,
CaseStatementWithTypeTest.JoinedChildA.class,
CaseStatementWithTypeTest.JoinedChildB.class,
CaseStatementWithTypeTest.JoinedDiscParent.class,
CaseStatementWithTypeTest.JoinedDiscChildA.class,
CaseStatementWithTypeTest.JoinedDiscChildB.class,
CaseStatementWithTypeTest.UnionParent.class,
CaseStatementWithTypeTest.UnionChildA.class,
CaseStatementWithTypeTest.UnionChildB.class,
@ -48,6 +52,8 @@ public class CaseStatementWithTypeTest {
session.persist( new SingleChildB( 2L ) );
session.persist( new JoinedChildA( 1L ) );
session.persist( new JoinedChildB( 2L ) );
session.persist( new JoinedDiscChildA( 1L ) );
session.persist( new JoinedDiscChildB( 2L ) );
session.persist( new UnionChildA( 1L ) );
session.persist( new UnionChildB( 2L ) );
} );
@ -58,6 +64,7 @@ public class CaseStatementWithTypeTest {
scope.inTransaction( session -> {
session.createMutationQuery( "delete from SingleParent" ).executeUpdate();
session.createMutationQuery( "delete from JoinedParent" ).executeUpdate();
session.createMutationQuery( "delete from JoinedDiscParent" ).executeUpdate();
session.createMutationQuery( "delete from UnionParent" ).executeUpdate();
} );
}
@ -74,6 +81,12 @@ public class CaseStatementWithTypeTest {
executeQuery( scope, JoinedParent.class, JoinedChildA.class, JoinedChildB.class, true );
}
@Test
public void testJoinedInheritanceAndDiscriminator(SessionFactoryScope scope) {
executeQuery( scope, JoinedDiscParent.class, JoinedDiscChildA.class, JoinedDiscChildB.class, false );
executeQuery( scope, JoinedDiscParent.class, JoinedDiscChildA.class, JoinedDiscChildB.class, true );
}
@Test
public void testTablePerClassInheritance(SessionFactoryScope scope) {
executeQuery( scope, UnionParent.class, UnionChildA.class, UnionChildB.class, false );
@ -178,6 +191,41 @@ public class CaseStatementWithTypeTest {
}
}
@Entity( name = "JoinedDiscParent" )
@Inheritance( strategy = InheritanceType.JOINED )
@DiscriminatorColumn
public static class JoinedDiscParent {
@Id
private Long id;
public JoinedDiscParent() {
}
public JoinedDiscParent(Long id) {
this.id = id;
}
}
@Entity( name = "JoinedDiscChildA" )
public static class JoinedDiscChildA extends JoinedDiscParent {
public JoinedDiscChildA() {
}
public JoinedDiscChildA(Long id) {
super( id );
}
}
@Entity( name = "JoinedDiscChildB" )
public static class JoinedDiscChildB extends JoinedDiscParent {
public JoinedDiscChildB() {
}
public JoinedDiscChildB(Long id) {
super( id );
}
}
@Entity( name = "UnionParent" )
@Inheritance( strategy = InheritanceType.TABLE_PER_CLASS )
public static class UnionParent {

View File

@ -0,0 +1,188 @@
/*
* 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.inheritance.discriminator;
import org.hibernate.testing.jdbc.SQLStatementInspector;
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.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Marco Belladelli
*/
@DomainModel( annotatedClasses = {
JoinedInheritanceDiscriminatorSelectionTest.ParentEntity.class,
JoinedInheritanceDiscriminatorSelectionTest.ChildA.class,
JoinedInheritanceDiscriminatorSelectionTest.SubChildA.class,
JoinedInheritanceDiscriminatorSelectionTest.ChildB.class,
} )
@SessionFactory( useCollectingStatementInspector = true )
@Jira( "https://hibernate.atlassian.net/browse/HHH-17727" )
public class JoinedInheritanceDiscriminatorSelectionTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.persist( new ParentEntity( 1L, "parent" ) );
session.persist( new ChildA( 2L, "child_a", 2 ) );
session.persist( new SubChildA( 3L, "sub_child_a", 3, 3 ) );
session.persist( new ChildB( 4L, "child_b" ) );
} );
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery( "delete from ParentEntity" ).executeUpdate() );
}
@Test
public void testSelectParentAttribute(SessionFactoryScope scope) {
final SQLStatementInspector inspector = scope.getCollectingStatementInspector();
inspector.clear();
scope.inTransaction( session -> {
assertThat( session.createQuery(
"select p.name from ParentEntity p",
String.class
).getResultList() ).hasSize( 4 );
inspector.assertNumberOfJoins( 0, 0 );
inspector.clear();
assertThat( session.createQuery(
"select p.name from ParentEntity p where type(p) = ParentEntity",
String.class
).getSingleResult() ).isEqualTo( "parent" );
inspector.assertNumberOfJoins( 0, 0 );
inspector.clear();
assertThat( session.createQuery(
"select p.name from ParentEntity p where type(p) = ChildA",
String.class
).getSingleResult() ).isEqualTo( "child_a" );
// We still inner-join the subtype table, this could be avoided since we have a discriminator condition
inspector.assertNumberOfJoins( 0, 1 );
} );
}
@Test
public void testSelectDiscriminator(SessionFactoryScope scope) {
final SQLStatementInspector inspector = scope.getCollectingStatementInspector();
inspector.clear();
scope.inTransaction( session -> {
assertThat( session.createQuery(
"select p.class from ParentEntity p",
String.class
).getResultList() ).hasSize( 4 );
inspector.assertNumberOfJoins( 0, 0 );
inspector.clear();
assertThat( session.createQuery(
"select type(p) from ParentEntity p",
String.class
).getResultList() ).hasSize( 4 );
inspector.assertNumberOfJoins( 0, 0 );
} );
}
@Test
public void testSelectInstance(SessionFactoryScope scope) {
final SQLStatementInspector inspector = scope.getCollectingStatementInspector();
inspector.clear();
scope.inTransaction( session -> {
// NOTE: we currently always join all subclasses when selecting the entity instance. We could
// maybe avoid this when we have a physical discriminator column and a type filter
assertThat( session.createQuery(
"from ParentEntity p where type(p) = ParentEntity",
ParentEntity.class
).getResultList() ).hasSize( 1 );
inspector.assertNumberOfJoins( 0, 3 );
inspector.clear();
assertThat( session.createQuery(
"from ParentEntity p where type(p) = ChildA",
ParentEntity.class
).getResultList() ).hasSize( 1 );
inspector.assertNumberOfJoins( 0, 3 );
inspector.clear();
assertThat( session.createQuery(
"from ParentEntity p where type(p) = SubChildA",
ParentEntity.class
).getResultList() ).hasSize( 1 );
inspector.assertNumberOfJoins( 0, 3 );
inspector.clear();
} );
}
@Entity( name = "ParentEntity" )
@Inheritance( strategy = InheritanceType.JOINED )
@DiscriminatorColumn( discriminatorType = DiscriminatorType.STRING )
public static class ParentEntity {
@Id
private Long id;
private String name;
public ParentEntity() {
}
public ParentEntity(Long id, String name) {
this.id = id;
this.name = name;
}
}
@Entity( name = "ChildA" )
public static class ChildA extends ParentEntity {
private Integer propertyA;
public ChildA() {
}
public ChildA(Long id, String name, Integer propertyA) {
super( id, name );
this.propertyA = propertyA;
}
}
@Entity( name = "SubChildA" )
public static class SubChildA extends ChildA {
private Integer subPropertyA;
public SubChildA() {
}
public SubChildA(Long id, String name, Integer propertyA, Integer subPropertyA) {
super( id, name, propertyA );
this.subPropertyA = subPropertyA;
}
}
@Entity( name = "ChildB" )
public static class ChildB extends ParentEntity {
public ChildB() {
}
public ChildB(Long id, String name) {
super( id, name );
}
}
}