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 .discriminator ;
8
+
9
+ import java .util .ArrayList ;
10
+ import java .util .List ;
11
+
12
+ import org .hibernate .Hibernate ;
13
+ import org .hibernate .annotations .DiscriminatorOptions ;
14
+
15
+ import org .hibernate .testing .jdbc .SQLStatementInspector ;
16
+ import org .hibernate .testing .orm .junit .DomainModel ;
17
+ import org .hibernate .testing .orm .junit .Jira ;
18
+ import org .hibernate .testing .orm .junit .SessionFactory ;
19
+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
20
+ import org .junit .jupiter .api .AfterAll ;
21
+ import org .junit .jupiter .api .BeforeAll ;
22
+ import org .junit .jupiter .api .Test ;
23
+
24
+ import jakarta .persistence .DiscriminatorColumn ;
25
+ import jakarta .persistence .DiscriminatorValue ;
26
+ import jakarta .persistence .Entity ;
27
+ import jakarta .persistence .FetchType ;
28
+ import jakarta .persistence .Id ;
29
+ import jakarta .persistence .Inheritance ;
30
+ import jakarta .persistence .InheritanceType ;
31
+ import jakarta .persistence .JoinColumn ;
32
+ import jakarta .persistence .OneToMany ;
33
+ import jakarta .persistence .Table ;
34
+
35
+ import static org .assertj .core .api .Assertions .assertThat ;
36
+
37
+ /**
38
+ * @author Andrea Boriero
39
+ * @author Marco Belladelli
40
+ */
41
+ @ DomainModel ( annotatedClasses = {
42
+ JoinedInheritanceForceDiscriminatorTest .CommonBase .class ,
43
+ JoinedInheritanceForceDiscriminatorTest .ElementEntity .class ,
44
+ JoinedInheritanceForceDiscriminatorTest .AnotherEntity .class ,
45
+ JoinedInheritanceForceDiscriminatorTest .ElementGroup .class
46
+ } )
47
+ @ SessionFactory ( useCollectingStatementInspector = true )
48
+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-17113" )
49
+ public class JoinedInheritanceForceDiscriminatorTest {
50
+ @ BeforeAll
51
+ public void setUp (SessionFactoryScope scope ) {
52
+ scope .inTransaction ( session -> {
53
+ final ElementEntity element = new ElementEntity ( 1L , "element_1" );
54
+ session .persist ( element );
55
+ final AnotherEntity another = new AnotherEntity ( 2L , "another_2" );
56
+ session .persist ( another );
57
+ final ElementGroup elementGroup = new ElementGroup ( 3L );
58
+ elementGroup .addElement ( element );
59
+ session .persist ( elementGroup );
60
+ } );
61
+ scope .inTransaction ( session -> {
62
+ // Emulate association with AnotherEntity on the same element_table
63
+ session .createNativeMutationQuery ( "update element_table set group_id = 3 where id = 2" ).executeUpdate ();
64
+ } );
65
+ }
66
+
67
+ @ AfterAll
68
+ public void tearDown (SessionFactoryScope scope ) {
69
+ scope .inTransaction ( session -> {
70
+ session .createMutationQuery ( "delete from CommonBase" ).executeUpdate ();
71
+ session .createMutationQuery ( "delete from ElementGroup" ).executeUpdate ();
72
+ } );
73
+ }
74
+
75
+ @ Test
76
+ public void testFindAndLoad (SessionFactoryScope scope ) {
77
+ final SQLStatementInspector inspector = scope .getCollectingStatementInspector ();
78
+ scope .inTransaction ( session -> {
79
+ final ElementGroup group = session .find ( ElementGroup .class , 3L );
80
+ inspector .clear ();
81
+ final List <ElementEntity > elements = group .getElements ();
82
+ assertThat ( Hibernate .isInitialized ( elements ) ).isFalse ();
83
+ assertThat ( elements ).hasSize ( 1 );
84
+ inspector .assertNumberOfOccurrenceInQueryNoSpace ( 0 , "disc_col" , 1 );
85
+ final CommonBase commonBase = elements .get ( 0 );
86
+ assertThat ( commonBase .getId () ).isEqualTo ( 1L );
87
+ assertThat ( commonBase .getName () ).isEqualTo ( "element_1" );
88
+ } );
89
+ }
90
+
91
+ @ Test
92
+ public void testQueryAndLoad (SessionFactoryScope scope ) {
93
+ final SQLStatementInspector inspector = scope .getCollectingStatementInspector ();
94
+ scope .inTransaction ( session -> {
95
+ final ElementGroup group = session .createQuery (
96
+ "from ElementGroup" ,
97
+ ElementGroup .class
98
+ ).getSingleResult ();
99
+ inspector .clear ();
100
+ final List <ElementEntity > elements = group .getElements ();
101
+ assertThat ( Hibernate .isInitialized ( elements ) ).isFalse ();
102
+ assertThat ( elements ).hasSize ( 1 );
103
+ inspector .assertNumberOfOccurrenceInQueryNoSpace ( 0 , "disc_col" , 1 );
104
+ final CommonBase commonBase = elements .get ( 0 );
105
+ assertThat ( commonBase .getId () ).isEqualTo ( 1L );
106
+ assertThat ( commonBase .getName () ).isEqualTo ( "element_1" );
107
+ } );
108
+ }
109
+
110
+ @ Test
111
+ public void testQueryAndJoinFetch (SessionFactoryScope scope ) {
112
+ final SQLStatementInspector inspector = scope .getCollectingStatementInspector ();
113
+ scope .inTransaction ( session -> {
114
+ final ElementGroup group = session .createQuery (
115
+ "from ElementGroup g join fetch g.elements" ,
116
+ ElementGroup .class
117
+ ).getSingleResult ();
118
+ inspector .assertNumberOfOccurrenceInQueryNoSpace ( 0 , "disc_col" , 1 );
119
+ final List <ElementEntity > elements = group .getElements ();
120
+ assertThat ( Hibernate .isInitialized ( elements ) ).isTrue ();
121
+ assertThat ( elements ).hasSize ( 1 );
122
+ final CommonBase commonBase = elements .get ( 0 );
123
+ assertThat ( commonBase .getId () ).isEqualTo ( 1L );
124
+ assertThat ( commonBase .getName () ).isEqualTo ( "element_1" );
125
+ } );
126
+ }
127
+
128
+ @ Inheritance ( strategy = InheritanceType .JOINED )
129
+ @ DiscriminatorColumn ( name = "disc_col" )
130
+ @ DiscriminatorOptions ( force = true )
131
+ @ Entity ( name = "CommonBase" )
132
+ public static class CommonBase {
133
+ @ Id
134
+ protected Long id ;
135
+
136
+ protected String name ;
137
+
138
+ public CommonBase () {
139
+ }
140
+
141
+ public CommonBase (Long id , String name ) {
142
+ this .id = id ;
143
+ this .name = name ;
144
+ }
145
+
146
+ public Long getId () {
147
+ return id ;
148
+ }
149
+
150
+ public String getName () {
151
+ return name ;
152
+ }
153
+ }
154
+
155
+ @ Entity ( name = "ElementEntity" )
156
+ @ DiscriminatorValue ( "element" )
157
+ @ Table ( name = "element_table" )
158
+ public static class ElementEntity extends CommonBase {
159
+ public ElementEntity () {
160
+ }
161
+
162
+ public ElementEntity (Long id , String name ) {
163
+ super ( id , name );
164
+ }
165
+ }
166
+
167
+ @ Entity ( name = "AnotherEntity" )
168
+ @ DiscriminatorValue ( "another" )
169
+ @ Table ( name = "element_table" )
170
+ public static class AnotherEntity extends CommonBase {
171
+ public AnotherEntity () {
172
+ }
173
+
174
+ public AnotherEntity (Long id , String name ) {
175
+ super ( id , name );
176
+ }
177
+ }
178
+
179
+ @ Entity ( name = "ElementGroup" )
180
+ public static class ElementGroup {
181
+ @ Id
182
+ protected Long id ;
183
+
184
+ @ OneToMany ( fetch = FetchType .LAZY )
185
+ @ JoinColumn ( name = "group_id" )
186
+ private List <ElementEntity > elements = new ArrayList <>();
187
+
188
+ public ElementGroup () {
189
+ }
190
+
191
+ public ElementGroup (Long id ) {
192
+ this .id = id ;
193
+ }
194
+
195
+ public Long getId () {
196
+ return id ;
197
+ }
198
+
199
+ public List <ElementEntity > getElements () {
200
+ return elements ;
201
+ }
202
+
203
+ public void addElement (ElementEntity element ) {
204
+ elements .add ( element );
205
+ }
206
+ }
207
+ }
0 commit comments