1414import org .hibernate .testing .orm .junit .Jira ;
1515import org .hibernate .testing .orm .junit .SessionFactory ;
1616import org .hibernate .testing .orm .junit .SessionFactoryScope ;
17+ import org .junit .jupiter .api .AfterAll ;
1718import org .junit .jupiter .api .BeforeAll ;
1819import org .junit .jupiter .api .Test ;
1920
2021import jakarta .persistence .DiscriminatorColumn ;
2122import jakarta .persistence .Entity ;
23+ import jakarta .persistence .GeneratedValue ;
2224import jakarta .persistence .Id ;
2325import jakarta .persistence .Inheritance ;
2426import jakarta .persistence .InheritanceType ;
2527import jakarta .persistence .JoinColumn ;
28+ import jakarta .persistence .ManyToOne ;
2629import jakarta .persistence .OneToMany ;
2730
2831import static org .assertj .core .api .Assertions .assertThat ;
2932
3033/**
3134 * @author Laurent Almeras
35+ * @author Marco Belladelli
3236 */
3337@ DomainModel ( annotatedClasses = {
34- ManyToOneInheritanceSubTypeTest .SuperType .class ,
35- ManyToOneInheritanceSubTypeTest .TypeA .class ,
36- ManyToOneInheritanceSubTypeTest .TypeB .class ,
37- ManyToOneInheritanceSubTypeTest .LinkedEntity .class
38+ ManyToOneInheritanceSubTypeTest .LinkedEntity .class ,
39+ ManyToOneInheritanceSubTypeTest .SingleTableEntity .class ,
40+ ManyToOneInheritanceSubTypeTest .SingleA .class ,
41+ ManyToOneInheritanceSubTypeTest .SubSingleA .class ,
42+ ManyToOneInheritanceSubTypeTest .SingleB .class ,
43+ ManyToOneInheritanceSubTypeTest .JoinedEntity .class ,
44+ ManyToOneInheritanceSubTypeTest .JoinedA .class ,
45+ ManyToOneInheritanceSubTypeTest .SubJoinedA .class ,
46+ ManyToOneInheritanceSubTypeTest .JoinedB .class ,
47+ ManyToOneInheritanceSubTypeTest .UnionEntity .class ,
48+ ManyToOneInheritanceSubTypeTest .UnionA .class ,
49+ ManyToOneInheritanceSubTypeTest .SubUnionA .class ,
50+ ManyToOneInheritanceSubTypeTest .UnionB .class ,
3851} )
3952@ SessionFactory ( useCollectingStatementInspector = true )
4053@ Jira ( "https://hibernate.atlassian.net/browse/HHH-16616" )
54+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-17483" )
4155public class ManyToOneInheritanceSubTypeTest {
4256 @ BeforeAll
4357 public void setUp (SessionFactoryScope scope ) {
4458 scope .inTransaction ( session -> {
45- final SuperType superType = new SuperType ( 1 );
46- final TypeB typeB = new TypeB ( 2 , "typeB" );
47- final TypeA typeA = new TypeA ( 3 , "typeA" );
48- final LinkedEntity entity = new LinkedEntity ( 4 );
49- entity .addTypeA ( typeA );
50- session .persist ( superType );
51- session .persist ( typeB );
52- session .persist ( typeA );
59+ final LinkedEntity entity = new LinkedEntity ( 1 );
60+ final SingleA singleA = new SingleA ();
61+ session .persist ( singleA );
62+ entity .getSingle ().add ( singleA );
63+ final SubSingleA subSingleA = new SubSingleA ();
64+ session .persist ( subSingleA );
65+ entity .getSingle ().add ( subSingleA );
66+ session .persist ( new SingleB () );
67+ final JoinedA joinedA = new JoinedA ();
68+ session .persist ( joinedA );
69+ entity .getJoined ().add ( joinedA );
70+ final SubJoinedA subJoinedA = new SubJoinedA ();
71+ session .persist ( subJoinedA );
72+ entity .getJoined ().add ( subJoinedA );
73+ session .persist ( new JoinedB () );
74+ final UnionA unionA = new UnionA ();
75+ unionA .setLinkedEntity ( entity );
76+ session .persist ( unionA );
77+ final SubUnionA subUnionA = new SubUnionA ();
78+ subUnionA .setLinkedEntity ( entity );
79+ session .persist ( subUnionA );
80+ session .persist ( new UnionB () );
5381 session .persist ( entity );
5482 } );
5583 }
5684
85+ @ AfterAll
86+ public void tearDown (SessionFactoryScope scope ) {
87+ scope .inTransaction ( session -> {
88+ session .createMutationQuery ( "delete from SingleTableEntity" ).executeUpdate ();
89+ session .createMutationQuery ( "delete from JoinedEntity" ).executeUpdate ();
90+ session .createMutationQuery ( "delete from UnionEntity" ).executeUpdate ();
91+ session .createMutationQuery ( "delete from LinkedEntity" ).executeUpdate ();
92+ } );
93+ }
94+
5795 @ Test
5896 public void testFind (SessionFactoryScope scope ) {
5997 final SQLStatementInspector inspector = scope .getCollectingStatementInspector ();
6098 inspector .clear ();
6199 scope .inTransaction ( session -> {
62- final LinkedEntity entity = session .find ( LinkedEntity .class , 4 );
63- assertThat ( entity .getTypeAS () ).hasSize ( 1 );
64- inspector .assertExecutedCount ( 2 );
65- inspector .assertNumberOfOccurrenceInQueryNoSpace ( 1 , "disc_col" , 1 );
100+ final LinkedEntity entity = session .find ( LinkedEntity .class , 1 );
101+ inspector .clear ();
102+ assertThat ( entity .getSingle () ).hasSize ( 2 );
103+ inspector .assertNumberOfOccurrenceInQueryNoSpace ( 0 , "disc_col" , 2 );
104+ assertThat ( entity .getJoined () ).hasSize ( 2 );
105+ assertThat ( entity .getUnion () ).hasSize ( 2 );
66106 } );
67107 }
68108
@@ -72,78 +112,140 @@ public void testJoinFetch(SessionFactoryScope scope) {
72112 inspector .clear ();
73113 scope .inTransaction ( session -> {
74114 final LinkedEntity entity = session .createQuery (
75- "from LinkedEntity e left join fetch e.typeAS" ,
115+ "from LinkedEntity e join fetch e.single" ,
116+ LinkedEntity .class
117+ ).getSingleResult ();
118+ assertThat ( entity .getSingle () ).hasSize ( 2 );
119+ inspector .assertExecutedCount ( 1 );
120+ inspector .assertNumberOfOccurrenceInQueryNoSpace ( 0 , "disc_col" , 2 );
121+ } );
122+ inspector .clear ();
123+ scope .inTransaction ( session -> {
124+ final LinkedEntity entity = session .createQuery (
125+ "from LinkedEntity e join fetch e.joined" ,
126+ LinkedEntity .class
127+ ).getSingleResult ();
128+ assertThat ( entity .getJoined () ).hasSize ( 2 );
129+ inspector .assertExecutedCount ( 1 );
130+ } );
131+ inspector .clear ();
132+ scope .inTransaction ( session -> {
133+ final LinkedEntity entity = session .createQuery (
134+ "from LinkedEntity e join fetch e.union" ,
76135 LinkedEntity .class
77136 ).getSingleResult ();
78- assertThat ( entity .getTypeAS () ).hasSize ( 1 );
137+ assertThat ( entity .getUnion () ).hasSize ( 2 );
79138 inspector .assertExecutedCount ( 1 );
80- inspector .assertNumberOfOccurrenceInQueryNoSpace ( 0 , "disc_col" , 1 );
81139 } );
82140 }
83141
84- @ Entity ( name = "SuperType" )
85- @ Inheritance ( strategy = InheritanceType .SINGLE_TABLE )
86- @ DiscriminatorColumn ( name = "disc_col" )
87- public static class SuperType {
142+ @ Entity ( name = "LinkedEntity" )
143+ public static class LinkedEntity {
88144 @ Id
89145 private Integer id ;
90146
91- public SuperType () {
147+ @ OneToMany
148+ @ JoinColumn ( name = "single_id" )
149+ private List <SingleA > single = new ArrayList <>();
150+
151+ @ OneToMany
152+ @ JoinColumn ( name = "joined_id" )
153+ private List <JoinedA > joined = new ArrayList <>();
154+
155+ @ OneToMany ( mappedBy = "linkedEntity" )
156+ private List <UnionA > union = new ArrayList <>();
157+
158+ public LinkedEntity () {
92159 }
93160
94- public SuperType (Integer id ) {
161+ public LinkedEntity (Integer id ) {
95162 this .id = id ;
96163 }
97- }
98164
99- @ Entity ( name = "TypeA" )
100- public static class TypeA extends SuperType {
101- private String typeAName ;
165+ public List < SingleA > getSingle () {
166+ return single ;
167+ }
102168
103- public TypeA () {
169+ public List <JoinedA > getJoined () {
170+ return joined ;
104171 }
105172
106- public TypeA (Integer id , String typeAName ) {
107- super ( id );
108- this .typeAName = typeAName ;
173+ public List <UnionA > getUnion () {
174+ return union ;
109175 }
110176 }
111177
112- @ Entity ( name = "TypeB" )
113- public static class TypeB extends SuperType {
114- private String typeBName ;
178+ // InheritanceType.SINGLE_TABLE
115179
116- public TypeB () {
117- }
180+ @ Entity ( name = "SingleTableEntity" )
181+ @ Inheritance ( strategy = InheritanceType .SINGLE_TABLE )
182+ @ DiscriminatorColumn ( name = "disc_col" )
183+ public static class SingleTableEntity {
184+ @ Id
185+ @ GeneratedValue
186+ private Integer id ;
187+ }
118188
119- public TypeB (Integer id , String typeBName ) {
120- super ( id );
121- this .typeBName = typeBName ;
122- }
189+ @ Entity ( name = "SingleA" )
190+ public static class SingleA extends SingleTableEntity {
123191 }
124192
125- @ Entity ( name = "LinkedEntity" )
126- public static class LinkedEntity {
193+ @ Entity ( name = "SubSingleA" )
194+ public static class SubSingleA extends SingleA {
195+ }
196+
197+ @ Entity ( name = "SingleB" )
198+ public static class SingleB extends SingleTableEntity {
199+ }
200+
201+ // InheritanceType.JOINED
202+
203+ @ Entity ( name = "JoinedEntity" )
204+ @ Inheritance ( strategy = InheritanceType .JOINED )
205+ public static class JoinedEntity {
127206 @ Id
207+ @ GeneratedValue
128208 private Integer id ;
209+ }
129210
130- @ OneToMany
131- @ JoinColumn ( name = "linked_id" )
132- private List < TypeA > typeAS = new ArrayList <>();
211+ @ Entity ( name = "JoinedA" )
212+ public static class JoinedA extends JoinedEntity {
213+ }
133214
134- public LinkedEntity () {
135- }
215+ @ Entity ( name = "SubJoinedA" )
216+ public static class SubJoinedA extends JoinedA {
217+ }
136218
137- public LinkedEntity ( Integer id ) {
138- this . id = id ;
139- }
219+ @ Entity ( name = "JoinedB" )
220+ public static class JoinedB extends JoinedEntity {
221+ }
140222
141- public List <TypeA > getTypeAS () {
142- return typeAS ;
143- }
223+ // InheritanceType.TABLE_PER_CLASS
224+
225+ @ Entity ( name = "UnionEntity" )
226+ @ Inheritance ( strategy = InheritanceType .TABLE_PER_CLASS )
227+ public static class UnionEntity {
228+ @ Id
229+ @ GeneratedValue
230+ private Integer id ;
231+ }
232+
233+ @ Entity ( name = "UnionA" )
234+ public static class UnionA extends UnionEntity {
235+ @ ManyToOne
236+ @ JoinColumn ( name = "linked_id" )
237+ private LinkedEntity linkedEntity ;
144238
145- public void addTypeA ( TypeA typeA ) {
146- this .typeAS . add ( typeA ) ;
239+ public void setLinkedEntity ( LinkedEntity linkedEntity ) {
240+ this .linkedEntity = linkedEntity ;
147241 }
148242 }
243+
244+ @ Entity ( name = "SubUnionA" )
245+ public static class SubUnionA extends UnionA {
246+ }
247+
248+ @ Entity ( name = "UnionB" )
249+ public static class UnionB extends UnionEntity {
250+ }
149251}
0 commit comments