Skip to content

Commit 8653c31

Browse files
committed
HHH-7573 : Improve fix readability and test case
1 parent 91995cf commit 8653c31

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

hibernate-entitymanager/src/main/java/org/hibernate/jpa/event/internal/core/JpaFlushEntityEventListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ private boolean copyState(Object entity, Type[] types, Object[] state, SessionFa
6262
int size = newState.length;
6363
boolean isDirty = false;
6464
for ( int index = 0; index < size ; index++ ) {
65-
if (state[index] == LazyPropertyInitializer.UNFETCHED_PROPERTY && newState[index] != LazyPropertyInitializer.UNFETCHED_PROPERTY
66-
|| state[index] != newState[index] && !types[index].isEqual(state[index], newState[index])) {
65+
if ( ( state[index] == LazyPropertyInitializer.UNFETCHED_PROPERTY &&
66+
newState[index] != LazyPropertyInitializer.UNFETCHED_PROPERTY ) ||
67+
( state[index] != newState[index] && !types[index].isEqual( state[index], newState[index] ) ) ) {
6768
isDirty = true;
6869
state[index] = newState[index];
6970
}

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/callbacks/CallbacksTest.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313

1414
import org.junit.Test;
1515

16+
import org.hibernate.Hibernate;
17+
import org.hibernate.internal.util.collections.ArrayHelper;
1618
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
1719
import org.hibernate.jpa.test.Cat;
1820
import org.hibernate.jpa.test.Kitten;
1921

2022
import org.hibernate.testing.FailureExpected;
23+
import org.hibernate.testing.TestForIssue;
2124

2225
import static org.junit.Assert.assertEquals;
2326
import static org.junit.Assert.assertFalse;
@@ -263,6 +266,7 @@ public Class[] getAnnotatedClasses() {
263266
* @throws Exception
264267
*/
265268
@Test
269+
@TestForIssue( jiraKey = "HHH-7573" )
266270
public void testJpaFlushEntityEventListener() throws Exception {
267271
EntityWithLazyProperty entity;
268272
EntityManager em = getOrCreateEntityManager();
@@ -276,51 +280,70 @@ public void testJpaFlushEntityEventListener() throws Exception {
276280
entity.setLazyData(testArray);
277281
em.persist(entity);
278282
em.getTransaction().commit();
283+
em.close();
284+
279285
checkLazyField(entity, em, testArray);
280286

281287
/**
282288
* Set a non lazy field, therefore the lazyData field will be LazyPropertyInitializer.UNFETCHED_PROPERTY
283289
* for both state and newState so the field should not change. This should no longer cause a ClassCastException.
284290
*/
291+
em = getOrCreateEntityManager();
285292
em.getTransaction().begin();
286293
entity = em.find(EntityWithLazyProperty.class, entity.getId());
287294
entity.setSomeField("TEST1");
295+
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
288296
em.getTransaction().commit();
297+
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
298+
em.close();
299+
289300
checkLazyField(entity, em, testArray);
290301

291302
/**
292303
* Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the
293304
* PreUpdate annotated callback method. So state == LazyPropertyInitializer.UNFETCHED_PROPERTY and
294305
* newState == EntityWithLazyProperty.PRE_UPDATE_VALUE. This should no longer cause a ClassCastException.
295306
*/
307+
em = getOrCreateEntityManager();
296308
em.getTransaction().begin();
297309
entity = em.find(EntityWithLazyProperty.class, entity.getId());
298310
entity.setUpdateLazyFieldInPreUpdate(true);
299311
entity.setSomeField("TEST2");
312+
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
300313
em.getTransaction().commit();
314+
assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
315+
em.close();
316+
301317
checkLazyField(entity, em, EntityWithLazyProperty.PRE_UPDATE_VALUE);
302318

303319
/**
304320
* Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the
305321
* PreUpdate annotated callback method and also set the lazyData field directly to testArray1. When we reload we
306322
* should get EntityWithLazyProperty.PRE_UPDATE_VALUE.
307323
*/
324+
em = getOrCreateEntityManager();
308325
em.getTransaction().begin();
309326
entity = em.find(EntityWithLazyProperty.class, entity.getId());
310327
entity.setUpdateLazyFieldInPreUpdate(true);
328+
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
311329
entity.setLazyData(testArray);
330+
assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
312331
entity.setSomeField("TEST3");
313332
em.getTransaction().commit();
314-
checkLazyField(entity, em, EntityWithLazyProperty.PRE_UPDATE_VALUE);
315-
316333
em.close();
334+
335+
checkLazyField( entity, em, EntityWithLazyProperty.PRE_UPDATE_VALUE);
317336
}
318337

319338
private void checkLazyField(EntityWithLazyProperty entity, EntityManager em, byte[] expected) {
320339
// reload the entity and check the lazy value matches what we expect.
340+
em = getOrCreateEntityManager();
321341
em.getTransaction().begin();
322342
entity = em.find(EntityWithLazyProperty.class, entity.getId());
323-
assertEquals(expected, entity.getLazyData());
343+
assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData") );
344+
assertTrue( ArrayHelper.isEquals( expected, entity.getLazyData() ) );
345+
assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) );
324346
em.getTransaction().commit();
347+
em.close();
325348
}
326349
}

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/callbacks/EntityWithLazyProperty.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
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+
*/
17
package org.hibernate.jpa.test.callbacks;
28

39
import javax.persistence.*;
410

511
/**
612
* Test entity with a lazy property which requires build time instrumentation.
13+
*
14+
* @author Martin Ball
715
*/
816
@Entity
917
public class EntityWithLazyProperty {

0 commit comments

Comments
 (0)