HHH-12868 Fix NPE when loading entity with CacheConcurrencyStrategy.NONE

This commit is contained in:
Guillaume Smet 2018-07-30 12:45:57 +02:00
parent 9ab285eb58
commit e43c374f30
2 changed files with 42 additions and 4 deletions

View File

@ -535,7 +535,7 @@ public abstract class AbstractEntityPersister
if ( creationContext.getSessionFactory().getSessionFactoryOptions().isSecondLevelCacheEnabled() ) { if ( creationContext.getSessionFactory().getSessionFactoryOptions().isSecondLevelCacheEnabled() ) {
this.canWriteToCache = determineCanWriteToCache( persistentClass, cacheAccessStrategy ); this.canWriteToCache = determineCanWriteToCache( persistentClass, cacheAccessStrategy );
this.canReadFromCache = determineCanReadFromCache( persistentClass ); this.canReadFromCache = determineCanReadFromCache( persistentClass, cacheAccessStrategy );
this.cacheAccessStrategy = cacheAccessStrategy; this.cacheAccessStrategy = cacheAccessStrategy;
this.isLazyPropertiesCacheable = persistentClass.getRootClass().isLazyPropertiesCacheable(); this.isLazyPropertiesCacheable = persistentClass.getRootClass().isLazyPropertiesCacheable();
this.naturalIdRegionAccessStrategy = naturalIdRegionAccessStrategy; this.naturalIdRegionAccessStrategy = naturalIdRegionAccessStrategy;
@ -916,7 +916,11 @@ public abstract class AbstractEntityPersister
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private boolean determineCanReadFromCache(PersistentClass persistentClass) { private boolean determineCanReadFromCache(PersistentClass persistentClass, EntityDataAccess cacheAccessStrategy) {
if ( cacheAccessStrategy == null ) {
return false;
}
if ( persistentClass.isCached() ) { if ( persistentClass.isCached() ) {
return true; return true;
} }

View File

@ -20,13 +20,16 @@ import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/** /**
* @author Chris Cranford * @author Chris Cranford
*/ */
@TestForIssue(jiraKey = "HHH-12587")
public class CacheAnnotationTests extends BaseCoreFunctionalTestCase { public class CacheAnnotationTests extends BaseCoreFunctionalTestCase {
private Integer entityId;
@Override @Override
protected void configure(Configuration configuration) { protected void configure(Configuration configuration) {
super.configure( configuration ); super.configure( configuration );
@ -39,7 +42,8 @@ public class CacheAnnotationTests extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testCacheConcurrencyStrategyNone() { @TestForIssue(jiraKey = "HHH-12587")
public void testCacheWriteConcurrencyStrategyNone() {
doInHibernate( this::sessionFactory, session -> { doInHibernate( this::sessionFactory, session -> {
NoCacheConcurrencyStrategyEntity entity = new NoCacheConcurrencyStrategyEntity(); NoCacheConcurrencyStrategyEntity entity = new NoCacheConcurrencyStrategyEntity();
session.save( entity ); 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") @Entity(name = "NoCacheConcurrencyStrategy")
@Cache(usage = CacheConcurrencyStrategy.NONE) @Cache(usage = CacheConcurrencyStrategy.NONE)
public static class NoCacheConcurrencyStrategyEntity { public static class NoCacheConcurrencyStrategyEntity {
@ -55,6 +79,8 @@ public class CacheAnnotationTests extends BaseCoreFunctionalTestCase {
@GeneratedValue @GeneratedValue
private Integer id; private Integer id;
private String name;
public Integer getId() { public Integer getId() {
return id; return id;
} }
@ -62,5 +88,13 @@ public class CacheAnnotationTests extends BaseCoreFunctionalTestCase {
public void setId(Integer id) { public void setId(Integer id) {
this.id = id; this.id = id;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} }
} }