66 */
77package org .hibernate .test .bytecode .enhancement .lazy .basic ;
88
9+ import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
10+ import org .hibernate .cfg .AvailableSettings ;
11+ import org .hibernate .cfg .Configuration ;
912import org .hibernate .orm .test .bytecode .enhancement .lazy .NoDirtyCheckingContext ;
1013
1114import static org .hibernate .testing .transaction .TransactionUtil .doInHibernate ;
1619import org .hibernate .testing .bytecode .enhancement .BytecodeEnhancerRunner ;
1720import org .hibernate .testing .bytecode .enhancement .CustomEnhancementContext ;
1821import org .hibernate .testing .bytecode .enhancement .EnhancerTestContext ;
22+ import org .hibernate .testing .jdbc .SQLStatementInspector ;
1923import org .hibernate .testing .junit4 .BaseCoreFunctionalTestCase ;
24+ import org .junit .Before ;
2025import org .junit .Test ;
2126import org .junit .runner .RunWith ;
2227
2934
3035@ RunWith (BytecodeEnhancerRunner .class )
3136@ CustomEnhancementContext ( {EnhancerTestContext .class , NoDirtyCheckingContext .class } )
32- @ TestForIssue (jiraKey = "HHH-15634" )
37+ @ TestForIssue (jiraKey = { "HHH-15634" , "HHH-16049" } )
3338public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
3439
3540 private Long entityId ;
@@ -39,6 +44,16 @@ public Class<?>[] getAnnotatedClasses() {
3944 return new Class <?>[] { LazyEntity .class };
4045 }
4146
47+ @ Override
48+ protected void prepareBasicRegistryBuilder (StandardServiceRegistryBuilder serviceRegistryBuilder ) {
49+ super .prepareBasicRegistryBuilder ( serviceRegistryBuilder );
50+ serviceRegistryBuilder .applySetting ( AvailableSettings .STATEMENT_INSPECTOR , SQLStatementInspector .class .getName () );
51+ }
52+
53+ SQLStatementInspector statementInspector () {
54+ return (SQLStatementInspector ) sessionFactory ().getSessionFactoryOptions ().getStatementInspector ();
55+ }
56+
4257 private void initNull () {
4358 doInHibernate ( this ::sessionFactory , s -> {
4459 LazyEntity entity = new LazyEntity ();
@@ -58,6 +73,23 @@ private void initNonNull() {
5873 } );
5974 }
6075
76+ @ Before
77+ public void clearStatementInspector () {
78+ statementInspector ().clear ();
79+ }
80+
81+ @ Test
82+ public void updateOneLazyProperty_nullToNull () {
83+ initNull ();
84+ doInHibernate ( this ::sessionFactory , s -> {
85+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
86+ entity .setLazyProperty1 ( null );
87+ } );
88+
89+ // We should not update entities when property values did not change
90+ statementInspector ().assertNoUpdate ();
91+ }
92+
6193 @ Test
6294 public void updateOneLazyProperty_nullToNonNull () {
6395 initNull ();
@@ -75,7 +107,7 @@ public void updateOneLazyProperty_nullToNonNull() {
75107 }
76108
77109 @ Test
78- public void updateOneLazyProperty_nonNullToNonNull () {
110+ public void updateOneLazyProperty_nonNullToNonNull_differentValues () {
79111 initNonNull ();
80112 doInHibernate ( this ::sessionFactory , s -> {
81113 LazyEntity entity = s .get ( LazyEntity .class , entityId );
@@ -90,6 +122,18 @@ public void updateOneLazyProperty_nonNullToNonNull() {
90122 } );
91123 }
92124
125+ @ Test
126+ public void updateOneLazyProperty_nonNullToNonNull_sameValues () {
127+ initNonNull ();
128+ doInHibernate ( this ::sessionFactory , s -> {
129+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
130+ entity .setLazyProperty1 ( "lazy1_update" );
131+ } );
132+
133+ // We should not update entities when property values did not change
134+ statementInspector ().assertNoUpdate ();
135+ }
136+
93137 @ Test
94138 public void updateOneLazyProperty_nonNullToNull () {
95139 initNonNull ();
@@ -106,6 +150,91 @@ public void updateOneLazyProperty_nonNullToNull() {
106150 } );
107151 }
108152
153+ @ Test
154+ public void updateOneEagerProperty_nullToNull () {
155+ initNull ();
156+ doInHibernate ( this ::sessionFactory , s -> {
157+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
158+ entity .setEagerProperty ( null );
159+ } );
160+
161+ // We should not update entities when property values did not change
162+ statementInspector ().assertNoUpdate ();
163+ }
164+
165+ @ Test
166+ public void updateOneEagerProperty_nullToNonNull () {
167+ initNull ();
168+ doInHibernate ( this ::sessionFactory , s -> {
169+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
170+ entity .setEagerProperty ( "eager_update" );
171+ } );
172+ doInHibernate ( this ::sessionFactory , s -> {
173+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
174+ assertEquals ( "eager_update" , entity .getEagerProperty () );
175+
176+ assertNull ( entity .getLazyProperty1 () );
177+ assertNull ( entity .getLazyProperty2 () );
178+ } );
179+ }
180+
181+ @ Test
182+ public void updateOneEagerProperty_nonNullToNonNull_differentValues () {
183+ initNonNull ();
184+ doInHibernate ( this ::sessionFactory , s -> {
185+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
186+ entity .setEagerProperty ( "eager_update" );
187+ } );
188+ doInHibernate ( this ::sessionFactory , s -> {
189+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
190+ assertEquals ( "eager_update" , entity .getEagerProperty () );
191+
192+ assertEquals ( "lazy1_initial" , entity .getLazyProperty1 () );
193+ assertEquals ( "lazy2_initial" , entity .getLazyProperty2 () );
194+ } );
195+ }
196+
197+ @ Test
198+ public void updateOneEagerProperty_nonNullToNonNull_sameValues () {
199+ initNonNull ();
200+ doInHibernate ( this ::sessionFactory , s -> {
201+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
202+ entity .setEagerProperty ( "eager_update" );
203+ } );
204+
205+ // We should not update entities when property values did not change
206+ statementInspector ().assertNoUpdate ();
207+ }
208+
209+ @ Test
210+ public void updateOneEagerProperty_nonNullToNull () {
211+ initNonNull ();
212+ doInHibernate ( this ::sessionFactory , s -> {
213+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
214+ entity .setEagerProperty ( null );
215+ } );
216+ doInHibernate ( this ::sessionFactory , s -> {
217+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
218+ assertNull ( entity .getEagerProperty () );
219+
220+ assertEquals ( "lazy1_initial" , entity .getLazyProperty1 () );
221+ assertEquals ( "lazy2_initial" , entity .getLazyProperty2 () );
222+ } );
223+ }
224+
225+ @ Test
226+ public void updateOneEagerPropertyAndOneLazyProperty_nullToNull () {
227+ initNull ();
228+ doInHibernate ( this ::sessionFactory , s -> {
229+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
230+ entity .setEagerProperty ( null );
231+ entity .setLazyProperty1 ( null );
232+ } );
233+
234+ // We should not update entities when property values did not change
235+ statementInspector ().assertNoUpdate ();
236+ }
237+
109238 @ Test
110239 public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull () {
111240 initNull ();
@@ -124,7 +253,7 @@ public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull() {
124253 }
125254
126255 @ Test
127- public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull () {
256+ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_differentValues () {
128257 initNonNull ();
129258 doInHibernate ( this ::sessionFactory , s -> {
130259 LazyEntity entity = s .get ( LazyEntity .class , entityId );
@@ -140,6 +269,19 @@ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull() {
140269 } );
141270 }
142271
272+ @ Test
273+ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_sameValues () {
274+ initNonNull ();
275+ doInHibernate ( this ::sessionFactory , s -> {
276+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
277+ entity .setEagerProperty ( entity .getEagerProperty () );
278+ entity .setLazyProperty1 ( entity .getLazyProperty1 () );
279+ } );
280+
281+ // We should not update entities when property values did not change
282+ statementInspector ().assertNoUpdate ();
283+ }
284+
143285 @ Test
144286 public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull () {
145287 initNonNull ();
@@ -157,6 +299,19 @@ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull() {
157299 } );
158300 }
159301
302+ @ Test
303+ public void updateAllLazyProperties_nullToNull () {
304+ initNull ();
305+ doInHibernate ( this ::sessionFactory , s -> {
306+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
307+ entity .setLazyProperty1 ( null );
308+ entity .setLazyProperty2 ( null );
309+ } );
310+
311+ // We should not update entities when property values did not change
312+ statementInspector ().assertNoUpdate ();
313+ }
314+
160315 @ Test
161316 public void updateAllLazyProperties_nullToNonNull () {
162317 initNull ();
@@ -175,7 +330,7 @@ public void updateAllLazyProperties_nullToNonNull() {
175330 }
176331
177332 @ Test
178- public void updateAllLazyProperties_nonNullToNonNull () {
333+ public void updateAllLazyProperties_nonNullToNonNull_differentValues () {
179334 initNonNull ();
180335 doInHibernate ( this ::sessionFactory , s -> {
181336 LazyEntity entity = s .get ( LazyEntity .class , entityId );
@@ -191,6 +346,19 @@ public void updateAllLazyProperties_nonNullToNonNull() {
191346 } );
192347 }
193348
349+ @ Test
350+ public void updateAllLazyProperties_nonNullToNonNull_sameValues () {
351+ initNonNull ();
352+ doInHibernate ( this ::sessionFactory , s -> {
353+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
354+ entity .setLazyProperty1 ( entity .getLazyProperty1 () );
355+ entity .setLazyProperty2 ( entity .getLazyProperty2 () );
356+ } );
357+
358+ // We should not update entities when property values did not change
359+ statementInspector ().assertNoUpdate ();
360+ }
361+
194362 @ Test
195363 public void updateAllLazyProperties_nonNullToNull () {
196364 initNonNull ();
0 commit comments