4
4
*/
5
5
package org .hibernate .orm .test .inheritance ;
6
6
7
- import java .util .List ;
8
-
9
- import org .hibernate .testing .orm .junit .DomainModel ;
10
- import org .hibernate .testing .orm .junit .Jira ;
11
- import org .hibernate .testing .orm .junit .SessionFactory ;
12
- import org .hibernate .testing .orm .junit .SessionFactoryScope ;
13
- import org .junit .jupiter .api .AfterAll ;
14
- import org .junit .jupiter .api .Test ;
15
-
16
7
import jakarta .persistence .Entity ;
17
8
import jakarta .persistence .FetchType ;
18
- import jakarta .persistence .GeneratedValue ;
19
9
import jakarta .persistence .Id ;
20
10
import jakarta .persistence .Inheritance ;
21
11
import jakarta .persistence .InheritanceType ;
22
12
import jakarta .persistence .ManyToMany ;
23
13
import jakarta .persistence .ManyToOne ;
14
+ import org .hibernate .testing .orm .junit .DomainModel ;
15
+ import org .hibernate .testing .orm .junit .Jira ;
16
+ import org .hibernate .testing .orm .junit .SessionFactory ;
17
+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
18
+ import org .junit .jupiter .api .AfterAll ;
19
+ import org .junit .jupiter .api .BeforeEach ;
20
+ import org .junit .jupiter .api .Test ;
21
+
22
+ import java .util .List ;
24
23
25
24
import static org .assertj .core .api .Assertions .assertThat ;
26
25
26
+ @ SuppressWarnings ("JUnitMalformedDeclaration" )
27
27
@ DomainModel ( annotatedClasses = {
28
28
// The user entities come first, so they get the lowest discriminator values
29
29
JoinedInheritanceCollectionSameHierarchyTest .UserEntity .class ,
39
39
public class JoinedInheritanceCollectionSameHierarchyTest {
40
40
@ Test
41
41
public void testGetDiscriminatorCollection (SessionFactoryScope scope ) {
42
- final Long id = scope .fromTransaction ( session -> {
43
- final UserEntity user = new UserEntity ( "test_user" );
44
- session .persist ( user );
45
- final GoodCompany company = new GoodCompany ();
46
- company .employee = user ;
47
- session .persist ( company );
48
- final CompanyRegistry companyRegistry = new CompanyRegistry ( List .of ( company ) );
49
- session .persist ( companyRegistry );
50
- return companyRegistry .id ;
51
- } );
52
42
scope .inSession ( session -> {
53
- final CompanyRegistry companyRegistry = session .get ( CompanyRegistry .class , id );
43
+ final CompanyRegistry companyRegistry = session .find ( CompanyRegistry .class , 30 );
54
44
assertThat ( companyRegistry .getCompanies () ).hasSize ( 1 )
55
45
.extracting ( AbstractCompany ::getEmployee )
56
46
.extracting ( UserEntity ::getName )
57
47
.containsOnly ( "test_user" );
58
48
} );
59
49
}
60
50
61
- @ AfterAll
62
- public void tearDown (SessionFactoryScope scope ) {
63
- scope .inTransaction ( session -> {
64
- session .createMutationQuery ( "delete from CompanyRegistry" ).executeUpdate ();
65
- session .createMutationQuery ( "delete from AbstractCompany" ).executeUpdate ();
66
- session .createMutationQuery ( "delete from SuperEntity" ).executeUpdate ();
51
+ @ BeforeEach
52
+ void createTestData (SessionFactoryScope sessions ) {
53
+ sessions .inTransaction ( session -> {
54
+ final UserEntity user = new UserEntity ( 1 , "test_user" );
55
+ session .persist ( user );
56
+ final GoodCompany company = new GoodCompany ( 20 );
57
+ company .employee = user ;
58
+ session .persist ( company );
59
+ final CompanyRegistry companyRegistry = new CompanyRegistry ( 30 , List .of ( company ) );
60
+ session .persist ( companyRegistry );
67
61
} );
68
62
}
69
63
64
+ @ AfterAll
65
+ public void dropTestData (SessionFactoryScope sessions ) {
66
+ sessions .dropData ();
67
+ }
68
+
70
69
@ Entity ( name = "SuperEntity" )
71
70
@ Inheritance ( strategy = InheritanceType .JOINED )
72
71
static abstract class SuperEntity {
73
72
@ Id
74
- @ GeneratedValue
75
- Long id ;
73
+ Integer id ;
74
+
75
+ public SuperEntity () {
76
+ }
77
+
78
+ public SuperEntity (Integer id ) {
79
+ this .id = id ;
80
+ }
76
81
}
77
82
78
83
@ Entity ( name = "CompanyRegistry" )
@@ -83,7 +88,8 @@ static class CompanyRegistry extends SuperEntity {
83
88
public CompanyRegistry () {
84
89
}
85
90
86
- public CompanyRegistry (List <AbstractCompany > companies ) {
91
+ public CompanyRegistry (Integer id , List <AbstractCompany > companies ) {
92
+ super ( id );
87
93
this .companies = companies ;
88
94
}
89
95
@@ -97,23 +103,50 @@ abstract static class AbstractCompany extends SuperEntity {
97
103
@ ManyToOne ( fetch = FetchType .LAZY )
98
104
UserEntity employee ;
99
105
106
+ public AbstractCompany () {
107
+ }
108
+
109
+ public AbstractCompany (Integer id ) {
110
+ super ( id );
111
+ }
112
+
100
113
public UserEntity getEmployee () {
101
114
return employee ;
102
115
}
103
116
}
104
117
105
118
@ Entity ( name = "GoodCompany" )
106
119
static class GoodCompany extends AbstractCompany {
120
+ public GoodCompany () {
121
+ }
122
+
123
+ public GoodCompany (Integer id ) {
124
+ super ( id );
125
+ }
107
126
}
108
127
109
128
@ Entity ( name = "BadCompany" )
110
129
static class BadCompany extends AbstractCompany {
111
130
// (unused) sibling subtype for 'GoodCompany'
131
+
132
+ public BadCompany () {
133
+ }
134
+
135
+ public BadCompany (Integer id ) {
136
+ super ( id );
137
+ }
112
138
}
113
139
114
140
@ Entity ( name = "AbstractUser" )
115
141
static class BaseUser extends SuperEntity {
116
142
// necessary intermediate entity so 'BaseUser' is the first child type and gets the lowest discriminator value
143
+
144
+ public BaseUser () {
145
+ }
146
+
147
+ public BaseUser (Integer id ) {
148
+ super ( id );
149
+ }
117
150
}
118
151
119
152
@ Entity ( name = "UserEntity" )
@@ -123,7 +156,8 @@ static class UserEntity extends BaseUser {
123
156
public UserEntity () {
124
157
}
125
158
126
- public UserEntity (String name ) {
159
+ public UserEntity (Integer id , String name ) {
160
+ super ( id );
127
161
this .name = name ;
128
162
}
129
163
0 commit comments