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() ) {
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;
}

View File

@ -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;
}
}
}