From 732abea8e54557993a1e48f8725ad8a2c2cd879f Mon Sep 17 00:00:00 2001 From: Janario Oliveira Date: Mon, 14 Mar 2016 14:06:34 -0300 Subject: [PATCH] HHH-10614 - Auto evict collection cache throws exception with null values --- .../internal/CollectionCacheInvalidator.java | 24 ++++++++++++------- .../cache/CollectionCacheEvictionTest.java | 20 ++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java index 4e11b256a7..c8cd460a3a 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java @@ -119,17 +119,11 @@ public class CollectionCacheInvalidator if ( oldState != null ) { // in case of updating an entity we perhaps have to decache 2 entity collections, this is the // old one - oldId = session.getIdentifier( oldState[i] ); + oldId = getIdentifier( session, oldState[i] ); } Object ref = persister.getPropertyValue( entity, i ); - Serializable id = null; - if ( ref != null ) { - id = session.getContextEntityIdentifier( ref ); - if ( id == null ) { - id = session.getSessionFactory().getClassMetadata( ref.getClass() ) - .getIdentifier( ref, session ); - } - } + Serializable id = getIdentifier( session, ref ); + // only evict if the related entity has changed if ( id != null && !id.equals( oldId ) ) { evict( id, collectionPersister, session ); @@ -159,6 +153,18 @@ public class CollectionCacheInvalidator } } + private Serializable getIdentifier(EventSource session, Object obj) { + Serializable id = null; + if ( obj != null ) { + id = session.getContextEntityIdentifier( obj ); + if ( id == null ) { + id = session.getSessionFactory().getClassMetadata( obj.getClass() ) + .getIdentifier( obj, session ); + } + } + return id; + } + private void evict(Serializable id, CollectionPersister collectionPersister, EventSource session) { if ( LOG.isDebugEnabled() ) { LOG.debug( "Evict CollectionRegion " + collectionPersister.getRole() + " for id " + id ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/CollectionCacheEvictionTest.java b/hibernate-core/src/test/java/org/hibernate/test/cache/CollectionCacheEvictionTest.java index 9c559cf98c..1ed58fa6c7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cache/CollectionCacheEvictionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/CollectionCacheEvictionTest.java @@ -283,4 +283,24 @@ public class CollectionCacheEvictionTest extends BaseCoreFunctionalTestCase { } s.close(); } + + @Test + public void testUpdateWithNullRelation() { + Session session = openSession(); + session.beginTransaction(); + User user = new User(); + user.setName( "User1" ); + session.persist( user ); + + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.beginTransaction(); + user.setName( "UserUpdate" ); + session.merge( user ); + + session.getTransaction().commit(); + session.close(); + } }