Skip to content

Commit 7678996

Browse files
committed
HHH-17292 Add test for issue
1 parent b455277 commit 7678996

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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.inheritance;
8+
9+
import org.hibernate.engine.spi.SessionImplementor;
10+
import org.hibernate.mapping.PersistentClass;
11+
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.Jira;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
19+
20+
import jakarta.persistence.DiscriminatorColumn;
21+
import jakarta.persistence.DiscriminatorType;
22+
import jakarta.persistence.DiscriminatorValue;
23+
import jakarta.persistence.Entity;
24+
import jakarta.persistence.GeneratedValue;
25+
import jakarta.persistence.Id;
26+
import jakarta.persistence.MappedSuperclass;
27+
import jakarta.persistence.Table;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* @author Marco Belladelli
33+
*/
34+
@DomainModel( annotatedClasses = {
35+
MultiLevelInheritanceQueryTest.AbstractRootEntity.class,
36+
MultiLevelInheritanceQueryTest.AbstractSuperclass.class,
37+
MultiLevelInheritanceQueryTest.ChildOneEntity.class,
38+
MultiLevelInheritanceQueryTest.ChildTwoEntity.class,
39+
} )
40+
@SessionFactory
41+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17292" )
42+
public class MultiLevelInheritanceQueryTest {
43+
@BeforeAll
44+
public void setUp(SessionFactoryScope scope) {
45+
scope.inTransaction( session -> {
46+
session.persist( new ChildTwoEntity( 1, "superclass_1", "child_one_1", "child_two_1" ) );
47+
session.persist( new ChildTwoEntity( 2, "superclass_2", "child_one_2", "child_two_2" ) );
48+
} );
49+
}
50+
51+
@AfterAll
52+
public void tearDown(SessionFactoryScope scope) {
53+
scope.inTransaction( session -> session.createMutationQuery( "delete from AbstractRootEntity" ).executeUpdate() );
54+
}
55+
56+
@Test
57+
public void testSelectionQuery(SessionFactoryScope scope) {
58+
scope.inTransaction( session -> {
59+
for ( PersistentClass pc : scope.getMetadataImplementor().getEntityBindings() ) {
60+
// Run test for each class in the inheritance tree
61+
final Class<?> entityClass = pc.getMappedClass();
62+
executeSelectionQuery( session, entityClass, "baseProp = 1" );
63+
executeSelectionQuery( session, entityClass, "mappedSuperclassProp = 'superclass_1'" );
64+
executeSelectionQuery( session, entityClass, "childOneProp = 'child_one_1'" );
65+
executeSelectionQuery( session, entityClass, "childTwoProp = 'child_two_1'" );
66+
}
67+
} );
68+
}
69+
70+
private void executeSelectionQuery(SessionImplementor session, Class<?> entityClass, String whereClause) {
71+
assertThat( session.createSelectionQuery(
72+
String.format( "from %s where %s", entityClass.getName(), whereClause ),
73+
entityClass
74+
).getResultList() ).hasSize( 1 );
75+
}
76+
77+
@Test
78+
public void testMutationQuery(SessionFactoryScope scope) {
79+
scope.inTransaction( session -> {
80+
for ( PersistentClass pc : scope.getMetadataImplementor().getEntityBindings() ) {
81+
// Run test for each class in the inheritance tree
82+
final Class<?> entityClass = pc.getMappedClass();
83+
executeMutationQuery( session, entityClass, "baseProp = 2" );
84+
executeMutationQuery( session, entityClass, "mappedSuperclassProp = 'superclass_2'" );
85+
executeMutationQuery( session, entityClass, "childOneProp = 'child_one_2'" );
86+
executeMutationQuery( session, entityClass, "childTwoProp = 'child_two_2'" );
87+
}
88+
} );
89+
}
90+
91+
private void executeMutationQuery(SessionImplementor session, Class<?> entityClass, String whereClause) {
92+
assertThat( session.createMutationQuery( String.format(
93+
"update %s set baseProp = 2 where %s",
94+
entityClass.getSimpleName(),
95+
whereClause
96+
) ).executeUpdate() ).isEqualTo( 1 );
97+
}
98+
99+
@Entity( name = "AbstractRootEntity" )
100+
@Table( name = "entity" )
101+
@DiscriminatorColumn( name = "disc_col", discriminatorType = DiscriminatorType.INTEGER )
102+
public abstract static class AbstractRootEntity {
103+
@Id
104+
@GeneratedValue
105+
private Long id;
106+
107+
private Integer baseProp;
108+
109+
public AbstractRootEntity() {
110+
}
111+
112+
public AbstractRootEntity(Integer baseProp) {
113+
this.baseProp = baseProp;
114+
}
115+
}
116+
117+
@MappedSuperclass
118+
public abstract static class AbstractSuperclass extends AbstractRootEntity {
119+
private String mappedSuperclassProp;
120+
121+
public AbstractSuperclass() {
122+
}
123+
124+
public AbstractSuperclass(Integer baseProp, String mappedSuperclassProp) {
125+
super( baseProp );
126+
this.mappedSuperclassProp = mappedSuperclassProp;
127+
}
128+
}
129+
130+
@Entity( name = "ChildOneEntity" )
131+
@DiscriminatorValue( "1" )
132+
public static class ChildOneEntity extends AbstractSuperclass {
133+
private String childOneProp;
134+
135+
public ChildOneEntity() {
136+
}
137+
138+
public ChildOneEntity(Integer baseProp, String mappedSuperclassProp, String childOneProp) {
139+
super( baseProp, mappedSuperclassProp );
140+
this.childOneProp = childOneProp;
141+
}
142+
}
143+
144+
@Entity( name = "ChildTwoEntity" )
145+
@DiscriminatorValue( "2" )
146+
public static class ChildTwoEntity extends ChildOneEntity {
147+
private String childTwoProp;
148+
149+
public ChildTwoEntity() {
150+
}
151+
152+
public ChildTwoEntity(Integer baseProp, String mappedSuperclassProp, String childOneProp, String childTwoProp) {
153+
super( baseProp, mappedSuperclassProp, childOneProp );
154+
this.childTwoProp = childTwoProp;
155+
}
156+
}
157+
}

0 commit comments

Comments
 (0)