12
12
import static org .junit .Assert .assertEquals ;
13
13
import static org .junit .Assert .assertNull ;
14
14
15
+ import org .hibernate .testing .TestForIssue ;
15
16
import org .hibernate .testing .bytecode .enhancement .BytecodeEnhancerRunner ;
16
17
import org .hibernate .testing .bytecode .enhancement .CustomEnhancementContext ;
17
18
import org .hibernate .testing .bytecode .enhancement .EnhancerTestContext ;
18
19
import org .hibernate .testing .junit4 .BaseCoreFunctionalTestCase ;
19
- import org .junit .Before ;
20
20
import org .junit .Test ;
21
21
import org .junit .runner .RunWith ;
22
22
29
29
30
30
@ RunWith (BytecodeEnhancerRunner .class )
31
31
@ CustomEnhancementContext ( {EnhancerTestContext .class , NoDirtyCheckingContext .class } )
32
- public class MultiLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
32
+ @ TestForIssue (jiraKey = "HHH-15634" )
33
+ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
33
34
34
35
private Long entityId ;
35
36
@@ -38,100 +39,175 @@ public Class<?>[] getAnnotatedClasses() {
38
39
return new Class <?>[] { LazyEntity .class };
39
40
}
40
41
41
- @ Before
42
- public void prepare () {
42
+ private void initNull () {
43
43
doInHibernate ( this ::sessionFactory , s -> {
44
44
LazyEntity entity = new LazyEntity ();
45
45
s .persist ( entity );
46
46
entityId = entity .getId ();
47
47
} );
48
48
}
49
49
50
+ private void initNonNull () {
51
+ doInHibernate ( this ::sessionFactory , s -> {
52
+ LazyEntity entity = new LazyEntity ();
53
+ entity .setEagerProperty ( "eager_initial" );
54
+ entity .setLazyProperty1 ( "lazy1_initial" );
55
+ entity .setLazyProperty2 ( "lazy2_initial" );
56
+ s .persist ( entity );
57
+ entityId = entity .getId ();
58
+ } );
59
+ }
60
+
50
61
@ Test
51
- public void updateOneLazyProperty () {
52
- // null -> non-null
62
+ public void updateOneLazyProperty_nullToNonNull () {
63
+ initNull ();
53
64
doInHibernate ( this ::sessionFactory , s -> {
54
65
LazyEntity entity = s .get ( LazyEntity .class , entityId );
55
- entity .setLazyProperty1 ( "update1 " );
66
+ entity .setLazyProperty1 ( "lazy1_update " );
56
67
} );
57
68
doInHibernate ( this ::sessionFactory , s -> {
58
69
LazyEntity entity = s .get ( LazyEntity .class , entityId );
59
- assertEquals ( "update1" , entity .getLazyProperty1 () );
70
+ assertEquals ( "lazy1_update" , entity .getLazyProperty1 () );
71
+
60
72
assertNull ( entity .getEagerProperty () );
61
73
assertNull ( entity .getLazyProperty2 () );
62
74
} );
75
+ }
63
76
64
- // non-null -> non-null
77
+ @ Test
78
+ public void updateOneLazyProperty_nonNullToNonNull () {
79
+ initNonNull ();
65
80
doInHibernate ( this ::sessionFactory , s -> {
66
81
LazyEntity entity = s .get ( LazyEntity .class , entityId );
67
- entity .setLazyProperty1 ( "update2 " );
82
+ entity .setLazyProperty1 ( "lazy1_update " );
68
83
} );
69
84
doInHibernate ( this ::sessionFactory , s -> {
70
85
LazyEntity entity = s .get ( LazyEntity .class , entityId );
71
- assertEquals ( "update2" , entity .getLazyProperty1 () );
72
- assertNull ( entity .getLazyProperty2 () );
73
- assertNull ( entity .getEagerProperty () );
86
+ assertEquals ( "lazy1_update" , entity .getLazyProperty1 () );
87
+
88
+ assertEquals ( "eager_initial" , entity .getEagerProperty () );
89
+ assertEquals ( "lazy2_initial" , entity .getLazyProperty2 () );
74
90
} );
75
91
}
76
92
77
93
@ Test
78
- public void updateOneEagerPropertyAndOneLazyProperty () {
79
- // null -> non-null
94
+ public void updateOneLazyProperty_nonNullToNull () {
95
+ initNonNull ();
80
96
doInHibernate ( this ::sessionFactory , s -> {
81
97
LazyEntity entity = s .get ( LazyEntity .class , entityId );
82
- entity .setEagerProperty ( "eager_update1" );
83
- entity .setLazyProperty1 ( "update1" );
98
+ entity .setLazyProperty1 ( null );
84
99
} );
85
100
doInHibernate ( this ::sessionFactory , s -> {
86
101
LazyEntity entity = s .get ( LazyEntity .class , entityId );
87
- assertEquals ( "eager_update1" , entity .getEagerProperty () );
88
- assertEquals ( "update1" , entity .getLazyProperty1 () );
89
- assertNull ( entity .getLazyProperty2 () );
102
+ assertNull ( entity .getLazyProperty1 () );
103
+
104
+ assertEquals ( "eager_initial" , entity .getEagerProperty () );
105
+ assertEquals ( "lazy2_initial" , entity .getLazyProperty2 () );
90
106
} );
107
+ }
91
108
92
- // non-null -> non-null
109
+ @ Test
110
+ public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull () {
111
+ initNull ();
93
112
doInHibernate ( this ::sessionFactory , s -> {
94
113
LazyEntity entity = s .get ( LazyEntity .class , entityId );
95
- entity .setEagerProperty ( "eager_update2 " );
96
- entity .setLazyProperty1 ( "update2 " );
114
+ entity .setEagerProperty ( "eager_update " );
115
+ entity .setLazyProperty1 ( "lazy1_update " );
97
116
} );
98
117
doInHibernate ( this ::sessionFactory , s -> {
99
118
LazyEntity entity = s .get ( LazyEntity .class , entityId );
100
- assertEquals ( "eager_update2" , entity .getEagerProperty () );
101
- assertEquals ( "update2" , entity .getLazyProperty1 () );
119
+ assertEquals ( "eager_update" , entity .getEagerProperty () );
120
+ assertEquals ( "lazy1_update" , entity .getLazyProperty1 () );
121
+
102
122
assertNull ( entity .getLazyProperty2 () );
103
123
} );
104
124
}
105
125
106
126
@ Test
107
- public void updateAllLazyProperties () {
108
- // null -> non-null
127
+ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull () {
128
+ initNonNull ();
109
129
doInHibernate ( this ::sessionFactory , s -> {
110
130
LazyEntity entity = s .get ( LazyEntity .class , entityId );
111
- entity .setLazyProperty1 ( "update1" );
112
- entity .setLazyProperty2 ( "update2_1" );
131
+ entity .setEagerProperty ( "eager_update" );
132
+ entity .setLazyProperty1 ( "lazy1_update" );
133
+ } );
134
+ doInHibernate ( this ::sessionFactory , s -> {
135
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
136
+ assertEquals ( "eager_update" , entity .getEagerProperty () );
137
+ assertEquals ( "lazy1_update" , entity .getLazyProperty1 () );
138
+
139
+ assertEquals ( "lazy2_initial" , entity .getLazyProperty2 () );
140
+ } );
141
+ }
142
+
143
+ @ Test
144
+ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull () {
145
+ initNonNull ();
146
+ doInHibernate ( this ::sessionFactory , s -> {
147
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
148
+ entity .setEagerProperty ( null );
149
+ entity .setLazyProperty1 ( null );
113
150
} );
114
151
doInHibernate ( this ::sessionFactory , s -> {
115
152
LazyEntity entity = s .get ( LazyEntity .class , entityId );
116
- assertEquals ( "update1" , entity .getLazyProperty1 () );
117
- assertEquals ( "update2_1" , entity .getLazyProperty2 () );
118
153
assertNull ( entity .getEagerProperty () );
154
+ assertNull ( entity .getLazyProperty1 () );
155
+
156
+ assertEquals ( "lazy2_initial" , entity .getLazyProperty2 () );
119
157
} );
158
+ }
120
159
121
- // non-null -> non-null
160
+ @ Test
161
+ public void updateAllLazyProperties_nullToNonNull () {
162
+ initNull ();
122
163
doInHibernate ( this ::sessionFactory , s -> {
123
164
LazyEntity entity = s .get ( LazyEntity .class , entityId );
124
- entity .setLazyProperty1 ( "update2 " );
125
- entity .setLazyProperty2 ( "update2_2 " );
165
+ entity .setLazyProperty1 ( "lazy1_update " );
166
+ entity .setLazyProperty2 ( "lazy2_update " );
126
167
} );
127
168
doInHibernate ( this ::sessionFactory , s -> {
128
169
LazyEntity entity = s .get ( LazyEntity .class , entityId );
129
- assertEquals ( "update2" , entity .getLazyProperty1 () );
130
- assertEquals ( "update2_2" , entity .getLazyProperty2 () );
170
+ assertEquals ( "lazy1_update" , entity .getLazyProperty1 () );
171
+ assertEquals ( "lazy2_update" , entity .getLazyProperty2 () );
172
+
131
173
assertNull ( entity .getEagerProperty () );
132
174
} );
133
175
}
134
176
177
+ @ Test
178
+ public void updateAllLazyProperties_nonNullToNonNull () {
179
+ initNonNull ();
180
+ doInHibernate ( this ::sessionFactory , s -> {
181
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
182
+ entity .setLazyProperty1 ( "lazy1_update" );
183
+ entity .setLazyProperty2 ( "lazy2_update" );
184
+ } );
185
+ doInHibernate ( this ::sessionFactory , s -> {
186
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
187
+ assertEquals ( "lazy1_update" , entity .getLazyProperty1 () );
188
+ assertEquals ( "lazy2_update" , entity .getLazyProperty2 () );
189
+
190
+ assertEquals ( "eager_initial" , entity .getEagerProperty () );
191
+ } );
192
+ }
193
+
194
+ @ Test
195
+ public void updateAllLazyProperties_nonNullToNull () {
196
+ initNonNull ();
197
+ doInHibernate ( this ::sessionFactory , s -> {
198
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
199
+ entity .setLazyProperty1 ( null );
200
+ entity .setLazyProperty2 ( null );
201
+ } );
202
+ doInHibernate ( this ::sessionFactory , s -> {
203
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
204
+ assertNull ( entity .getLazyProperty1 () );
205
+ assertNull ( entity .getLazyProperty2 () );
206
+
207
+ assertEquals ( "eager_initial" , entity .getEagerProperty () );
208
+ } );
209
+ }
210
+
135
211
@ Entity
136
212
@ Table (name = "LAZY_ENTITY" )
137
213
private static class LazyEntity {
0 commit comments