HHH-8949 @QueryHint( name="javax.persistence.cache.retrieveMode", value="<valid value>") does not work

This commit is contained in:
Henady Zakalusky 2014-06-10 09:31:02 +03:00 committed by Brett Meyer
parent 72e9645d4b
commit 0f85e8476b
2 changed files with 44 additions and 24 deletions

View File

@ -315,28 +315,14 @@ public abstract class BaseQueryImpl implements Query {
applied = applyFlushModeHint( ConfigurationHelper.getFlushMode( value ) );
}
else if ( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE.equals( hintName ) ) {
final CacheRetrieveMode retrieveMode = (CacheRetrieveMode) value;
CacheStoreMode storeMode = hints != null
? (CacheStoreMode) hints.get( AvailableSettings.SHARED_CACHE_STORE_MODE )
: null;
if ( storeMode == null ) {
storeMode = (CacheStoreMode) entityManager.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE );
}
final CacheRetrieveMode retrieveMode = value != null ? CacheRetrieveMode.valueOf( value.toString() ) : null;
final CacheStoreMode storeMode = getHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.class );
applied = applyCacheModeHint( CacheModeHelper.interpretCacheMode( storeMode, retrieveMode ) );
}
else if ( AvailableSettings.SHARED_CACHE_STORE_MODE.equals( hintName ) ) {
final CacheStoreMode storeMode = (CacheStoreMode) value;
CacheRetrieveMode retrieveMode = hints != null
? (CacheRetrieveMode) hints.get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE )
: null;
if ( retrieveMode == null ) {
retrieveMode = (CacheRetrieveMode) entityManager.getProperties().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE );
}
applied = applyCacheModeHint(
CacheModeHelper.interpretCacheMode( storeMode, retrieveMode )
);
final CacheStoreMode storeMode = value != null ? CacheStoreMode.valueOf( value.toString () ) : null;
final CacheRetrieveMode retrieveMode = getHint( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE, CacheRetrieveMode.class );
applied = applyCacheModeHint( CacheModeHelper.interpretCacheMode( storeMode, retrieveMode ) );
}
else if ( QueryHints.HINT_NATIVE_LOCKMODE.equals( hintName ) ) {
if ( !isNativeSqlQuery() ) {
@ -411,6 +397,16 @@ public abstract class BaseQueryImpl implements Query {
return this;
}
private <T extends Enum<T>> T getHint(String key, Class<T> hintClass) {
Object hint = hints != null ? hints.get( key ) : null;
if ( hint == null ) {
hint = entityManager.getProperties().get( key );
}
return hint != null ? Enum.valueOf( hintClass, hint.toString() ) : null;
}
/**
* Is the query represented here a native SQL query?
*

View File

@ -30,11 +30,7 @@ import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import javax.persistence.Tuple;
import javax.persistence.*;
import org.junit.Test;
@ -189,6 +185,34 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
em.close();
}
@Test
@TestForIssue( jiraKey = "HHH_8949" )
public void testCacheStoreAndRetrieveModeParameter() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Query query = em.createQuery( "select item from Item item" );
query.getHints().clear();
query.setHint( "javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE );
query.setHint( "javax.persistence.cache.storeMode", CacheStoreMode.REFRESH );
assertEquals( CacheRetrieveMode.USE, query.getHints().get( "javax.persistence.cache.retrieveMode" ) );
assertEquals( CacheStoreMode.REFRESH, query.getHints().get( "javax.persistence.cache.storeMode" ) );
query.getHints().clear();
query.setHint( "javax.persistence.cache.retrieveMode", "USE" );
query.setHint( "javax.persistence.cache.storeMode", "REFRESH" );
assertEquals( "USE", query.getHints().get( "javax.persistence.cache.retrieveMode" ) );
assertEquals( "REFRESH", query.getHints().get( "javax.persistence.cache.storeMode" ) );
em.getTransaction().commit();
em.close();
}
@Test
public void testJpaPositionalParameters() {
EntityManager em = getOrCreateEntityManager();