From e43c374f30a656a428db551746cbc23bf9c5877c Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Mon, 30 Jul 2018 12:45:57 +0200 Subject: [PATCH] HHH-12868 Fix NPE when loading entity with CacheConcurrencyStrategy.NONE --- .../entity/AbstractEntityPersister.java | 8 +++- .../test/cache/CacheAnnotationTests.java | 38 ++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 1204fa1e97..dd87b91f25 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -535,7 +535,7 @@ public abstract class AbstractEntityPersister if ( creationContext.getSessionFactory().getSessionFactoryOptions().isSecondLevelCacheEnabled() ) { this.canWriteToCache = determineCanWriteToCache( persistentClass, cacheAccessStrategy ); - this.canReadFromCache = determineCanReadFromCache( persistentClass ); + this.canReadFromCache = determineCanReadFromCache( persistentClass, cacheAccessStrategy ); this.cacheAccessStrategy = cacheAccessStrategy; this.isLazyPropertiesCacheable = persistentClass.getRootClass().isLazyPropertiesCacheable(); this.naturalIdRegionAccessStrategy = naturalIdRegionAccessStrategy; @@ -916,7 +916,11 @@ public abstract class AbstractEntityPersister } @SuppressWarnings("unchecked") - private boolean determineCanReadFromCache(PersistentClass persistentClass) { + private boolean determineCanReadFromCache(PersistentClass persistentClass, EntityDataAccess cacheAccessStrategy) { + if ( cacheAccessStrategy == null ) { + return false; + } + if ( persistentClass.isCached() ) { return true; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/CacheAnnotationTests.java b/hibernate-core/src/test/java/org/hibernate/test/cache/CacheAnnotationTests.java index d4d219edc3..4c6340623c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cache/CacheAnnotationTests.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/CacheAnnotationTests.java @@ -20,13 +20,16 @@ import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; /** * @author Chris Cranford */ -@TestForIssue(jiraKey = "HHH-12587") public class CacheAnnotationTests extends BaseCoreFunctionalTestCase { + private Integer entityId; + @Override protected void configure(Configuration configuration) { super.configure( configuration ); @@ -39,7 +42,8 @@ public class CacheAnnotationTests extends BaseCoreFunctionalTestCase { } @Test - public void testCacheConcurrencyStrategyNone() { + @TestForIssue(jiraKey = "HHH-12587") + public void testCacheWriteConcurrencyStrategyNone() { doInHibernate( this::sessionFactory, session -> { NoCacheConcurrencyStrategyEntity entity = new NoCacheConcurrencyStrategyEntity(); session.save( entity ); @@ -48,6 +52,26 @@ public class CacheAnnotationTests extends BaseCoreFunctionalTestCase { } ); } + @Test + @TestForIssue(jiraKey = "HHH-12868") + public void testCacheReadConcurrencyStrategyNone() { + doInHibernate( this::sessionFactory, session -> { + NoCacheConcurrencyStrategyEntity entity = new NoCacheConcurrencyStrategyEntity(); + entity.setName( "name" ); + session.save( entity ); + session.flush(); + + this.entityId = entity.getId(); + + session.clear(); + } ); + + doInHibernate( this::sessionFactory, session -> { + NoCacheConcurrencyStrategyEntity entity = session.load( NoCacheConcurrencyStrategyEntity.class, this.entityId ); + assertEquals( "name", entity.getName() ); + } ); + } + @Entity(name = "NoCacheConcurrencyStrategy") @Cache(usage = CacheConcurrencyStrategy.NONE) public static class NoCacheConcurrencyStrategyEntity { @@ -55,6 +79,8 @@ public class CacheAnnotationTests extends BaseCoreFunctionalTestCase { @GeneratedValue private Integer id; + private String name; + public Integer getId() { return id; } @@ -62,5 +88,13 @@ public class CacheAnnotationTests extends BaseCoreFunctionalTestCase { public void setId(Integer id) { this.id = id; } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } } }