[hibernate-commits] Hibernate SVN: r18603 - in core/trunk: entitymanager/src/main/java/org/hibernate/ejb and 4 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Jan 21 20:55:20 EST 2010


Author: steve.ebersole at jboss.com
Date: 2010-01-21 20:55:20 -0500 (Thu, 21 Jan 2010)
New Revision: 18603

Added:
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/util/CacheModeHelper.java
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/cachemodes/
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/cachemodes/SharedCacheModesTest.java
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/cachemodes/SimpleEntity.java
Modified:
   core/trunk/core/src/main/java/org/hibernate/impl/AbstractQueryImpl.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractQueryImpl.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java
Log:
HHH-4660 - Support Cache Retrieve Mode and Cache Store Mode Properties


Modified: core/trunk/core/src/main/java/org/hibernate/impl/AbstractQueryImpl.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/impl/AbstractQueryImpl.java	2010-01-21 20:48:59 UTC (rev 18602)
+++ core/trunk/core/src/main/java/org/hibernate/impl/AbstractQueryImpl.java	2010-01-22 01:55:20 UTC (rev 18603)
@@ -144,6 +144,10 @@
 		return this;
 	}
 
+	public CacheMode getCacheMode() {
+		return cacheMode;
+	}
+
 	public Query setCacheable(boolean cacheable) {
 		this.cacheable = cacheable;
 		return this;

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java	2010-01-21 20:48:59 UTC (rev 18602)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java	2010-01-22 01:55:20 UTC (rev 18603)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
 /*
  * Hibernate, Relational Persistence for Idiomatic Java
  *
@@ -33,6 +33,8 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import javax.persistence.CacheRetrieveMode;
+import javax.persistence.CacheStoreMode;
 import javax.persistence.EntityNotFoundException;
 import javax.persistence.EntityTransaction;
 import javax.persistence.FlushModeType;
@@ -66,6 +68,7 @@
 import org.slf4j.LoggerFactory;
 
 import org.hibernate.AssertionFailure;
+import org.hibernate.CacheMode;
 import org.hibernate.FlushMode;
 import org.hibernate.HibernateException;
 import org.hibernate.LockMode;
@@ -87,6 +90,7 @@
 import org.hibernate.ejb.criteria.ValueHandlerFactory;
 import org.hibernate.ejb.criteria.expression.CompoundSelectionImpl;
 import org.hibernate.ejb.transaction.JoinableCMTTransaction;
+import org.hibernate.ejb.util.CacheModeHelper;
 import org.hibernate.ejb.util.ConfigurationHelper;
 import org.hibernate.engine.SessionFactoryImplementor;
 import org.hibernate.engine.SessionImplementor;
@@ -110,6 +114,8 @@
 		entityManagerSpecificProperties.add( AvailableSettings.LOCK_SCOPE );
 		entityManagerSpecificProperties.add( AvailableSettings.LOCK_TIMEOUT );
 		entityManagerSpecificProperties.add( AvailableSettings.FLUSH_MODE );
+		entityManagerSpecificProperties.add( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE );
+		entityManagerSpecificProperties.add( AvailableSettings.SHARED_CACHE_STORE_MODE );
 	}
 
 	private EntityManagerFactoryImpl entityManagerFactory;
@@ -152,8 +158,30 @@
 	private void applyProperties() {
 		getSession().setFlushMode( ConfigurationHelper.getFlushMode( properties.get( AvailableSettings.FLUSH_MODE ) ) );
 		setLockOptions(this.properties, this.lockOptions);
+		getSession().setCacheMode(
+				CacheModeHelper.interpretCacheMode(
+						currentCacheStoreMode(),
+						currentCacheRetrieveMode()
+				)
+		);
 	}
 
+	private CacheRetrieveMode currentCacheRetrieveMode() {
+		return determineCacheRetrieveMode( properties );
+	}
+
+	private CacheRetrieveMode determineCacheRetrieveMode(Map<String,Object> settings) {
+		return (CacheRetrieveMode) settings.get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE );
+	}
+
+	private CacheStoreMode currentCacheStoreMode() {
+		return determineCacheStoreMode( properties );
+	}
+
+	private CacheStoreMode determineCacheStoreMode(Map<String,Object> settings) {
+		return (CacheStoreMode) properties.get( AvailableSettings.SHARED_CACHE_STORE_MODE );
+	}
+
 	private void setLockOptions(Map<String, Object> props, LockOptions options) {
 		Object lockScope = props.get( AvailableSettings.LOCK_SCOPE );
 		if ( lockScope instanceof String && PessimisticLockScope.valueOf( ( String ) lockScope ) == PessimisticLockScope.EXTENDED ) {
@@ -194,15 +222,21 @@
 	 * set.
 	 */
 	private void setDefaultProperties() {
-		if (properties.get( AvailableSettings.FLUSH_MODE ) == null) {
+		if ( properties.get( AvailableSettings.FLUSH_MODE ) == null) {
 			properties.put( AvailableSettings.FLUSH_MODE, getSession().getFlushMode().toString() );
 		}
-		if (properties.get( AvailableSettings.LOCK_SCOPE ) == null) {
+		if ( properties.get( AvailableSettings.LOCK_SCOPE ) == null) {
 			this.properties.put( AvailableSettings.LOCK_SCOPE, PessimisticLockScope.EXTENDED.name() );
 		}
-		if (properties.get( AvailableSettings.LOCK_TIMEOUT ) == null) {
+		if ( properties.get( AvailableSettings.LOCK_TIMEOUT ) == null) {
 			properties.put( AvailableSettings.LOCK_TIMEOUT, LockOptions.WAIT_FOREVER );
 		}
+		if ( properties.get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) == null) {
+			properties.put( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE, CacheModeHelper.DEFAULT_RETRIEVE_MODE );
+		}
+		if ( properties.get( AvailableSettings.SHARED_CACHE_STORE_MODE ) == null) {
+			properties.put( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheModeHelper.DEFAULT_STORE_MODE );
+		}
 	}
 
 	public Query createQuery(String jpaqlString) {
@@ -484,8 +518,11 @@
 	}
 
 	public <A> A find(Class<A> entityClass, Object  primaryKey, LockModeType lockModeType, Map<String, Object> properties) {
+		CacheMode previousCacheMode = getSession().getCacheMode();
+		CacheMode cacheMode = determineAppropriateLocalCacheMode( properties );
 		LockOptions lockOptions = null;
 		try {
+			getSession().setCacheMode( cacheMode );
 			if ( lockModeType != null ) {
 				return ( A ) getSession().get(
 						entityClass, ( Serializable ) primaryKey,
@@ -515,8 +552,29 @@
 		catch ( HibernateException he ) {
 			throw convert( he , lockOptions );
 		}
+		finally {
+			getSession().setCacheMode( previousCacheMode );
+		}
 	}
 
+	public CacheMode determineAppropriateLocalCacheMode(Map<String, Object> localProperties) {
+		CacheRetrieveMode retrieveMode = null;
+		CacheStoreMode storeMode = null;
+		if ( localProperties != null ) {
+			retrieveMode = determineCacheRetrieveMode( localProperties );
+			storeMode = determineCacheStoreMode( localProperties );
+		}
+		if ( retrieveMode == null ) {
+			// use the EM setting
+			retrieveMode = determineCacheRetrieveMode( this.properties );
+		}
+		if ( storeMode == null ) {
+			// use the EM setting
+			storeMode = determineCacheStoreMode( this.properties );
+		}
+		return CacheModeHelper.interpretCacheMode( storeMode, retrieveMode );
+	}
+
 	private void checkTransactionNeeded() {
 		if ( persistenceContextType == PersistenceContextType.TRANSACTION && !isTransactionInProgress() ) {
 			//no need to mark as rollback, no tx in progress
@@ -583,8 +641,11 @@
 
 	public void refresh(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
 		checkTransactionNeeded();
+		CacheMode previousCacheMode = getSession().getCacheMode();
+		CacheMode localCacheMode = determineAppropriateLocalCacheMode( properties );
 		LockOptions lockOptions = null;
 		try {
+			getSession().setCacheMode( localCacheMode );
 			if ( !getSession().contains( entity ) ) {
 				throw new IllegalArgumentException( "Entity not managed" );
 			}
@@ -599,6 +660,9 @@
 		catch ( HibernateException he ) {
 			throw convert( he, lockOptions);
 		}
+		finally {
+			getSession().setCacheMode( previousCacheMode );
+		}
 	}
 
 	public boolean contains(Object entity) {

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractQueryImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractQueryImpl.java	2010-01-21 20:48:59 UTC (rev 18602)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractQueryImpl.java	2010-01-22 01:55:20 UTC (rev 18603)
@@ -27,6 +27,8 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import javax.persistence.CacheRetrieveMode;
+import javax.persistence.CacheStoreMode;
 import javax.persistence.FlushModeType;
 import javax.persistence.Parameter;
 import javax.persistence.TransactionRequiredException;
@@ -47,6 +49,8 @@
 import static org.hibernate.ejb.QueryHints.HINT_FLUSH_MODE;
 import static org.hibernate.ejb.QueryHints.HINT_READONLY;
 import static org.hibernate.ejb.QueryHints.HINT_TIMEOUT;
+
+import org.hibernate.ejb.util.CacheModeHelper;
 import org.hibernate.ejb.util.ConfigurationHelper;
 import org.hibernate.hql.QueryExecutionRequestException;
 
@@ -215,6 +219,34 @@
 			else if ( HINT_FLUSH_MODE.equals( hintName ) ) {
 				applyFlushMode( 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 );
+				}
+				applyCacheMode(
+						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 );
+				}
+				applyCacheMode(
+						CacheModeHelper.interpretCacheMode( storeMode, retrieveMode )
+				);
+			}
 			/* TODO:
 			else if ( "org.hibernate.lockMode".equals( hintName ) ) {
 				query.setAliasLockMode( alias, lockMode );

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java	2010-01-21 20:48:59 UTC (rev 18602)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java	2010-01-22 01:55:20 UTC (rev 18603)
@@ -113,14 +113,36 @@
 	 * enabled as per the rules defined in JPA 2 section 3.1.7.
 	 * <p/>
 	 * See JPA 2 sections 9.4.3 and 8.2.1.7
+	 * @see javax.persistence.SharedCacheMode
 	 */
 	public static final String SHARED_CACHE_MODE = "javax.persistence.sharedCache.mode";
 
 	/**
+	 * NOTE : Not a valid EMF property...
+	 * <p/>
+	 * Used to indicate if the provider should attempt to retrieve requested data
+	 * in the shared cache.
+	 *
+	 * @see javax.persistence.CacheRetrieveMode
+	 */
+	public static final String SHARED_CACHE_RETRIEVE_MODE ="javax.persistence.cache.retrieveMode";
+
+	/**
+	 * NOTE : Not a valid EMF property...
+	 * <p/>
+	 * Used to indicate if the provider should attempt to store data loaded from the database
+	 * in the shared cache.
+	 *
+	 * @see javax.persistence.CacheStoreMode
+	 */
+	public static final String SHARED_CACHE_STORE_MODE ="javax.persistence.cache.storeMode";
+
+	/**
 	 * Used to indicate what form of automatic validation is in effect as per rules defined
 	 * in JPA 2 section 3.6.1.1
 	 * <p/>
 	 * See JPA 2 sections 9.4.3 and 8.2.1.8
+	 * @see javax.persistence.ValidationMode
 	 */
 	public static final String VALIDATION_MODE = "javax.persistence.validation.mode";
 

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java	2010-01-21 20:48:59 UTC (rev 18602)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java	2010-01-22 01:55:20 UTC (rev 18603)
@@ -28,6 +28,7 @@
 import javax.persistence.TypedQuery;
 import javax.persistence.criteria.Selection;
 
+import org.hibernate.CacheMode;
 import org.hibernate.HibernateException;
 import org.hibernate.StaleStateException;
 import org.hibernate.LockOptions;

Added: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/util/CacheModeHelper.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/util/CacheModeHelper.java	                        (rev 0)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/util/CacheModeHelper.java	2010-01-22 01:55:20 UTC (rev 18603)
@@ -0,0 +1,100 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.ejb.util;
+
+import javax.persistence.CacheRetrieveMode;
+import javax.persistence.CacheStoreMode;
+
+import org.hibernate.CacheMode;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class CacheModeHelper {
+	public static final CacheMode DEFAULT_LEGACY_MODE = CacheMode.NORMAL;
+	public static final CacheStoreMode DEFAULT_STORE_MODE = CacheStoreMode.USE;
+	public static final CacheRetrieveMode DEFAULT_RETRIEVE_MODE = CacheRetrieveMode.USE;
+
+	/**
+	 * Given a JPA {@link CacheStoreMode} and {@link CacheRetrieveMode}, determine the corresponding
+	 * legacy Hibernate {@link CacheMode}.
+	 *
+	 * @param storeMode The JPA shared-cache store mode.
+	 * @param retrieveMode The JPA shared-cache retrieve mode.
+	 *
+	 * @return Corresponding {@link CacheMode}.
+	 */
+	public static CacheMode interpretCacheMode(CacheStoreMode storeMode, CacheRetrieveMode retrieveMode) {
+		if ( storeMode == null ) {
+			storeMode = DEFAULT_STORE_MODE;
+		}
+		if ( retrieveMode == null ) {
+			retrieveMode = DEFAULT_RETRIEVE_MODE;
+		}
+
+		final boolean get = ( CacheRetrieveMode.USE == retrieveMode );
+
+		switch ( storeMode ) {
+			case USE: {
+				return get ? CacheMode.NORMAL : CacheMode.PUT;
+			}
+			case REFRESH: {
+				// really (get == true) here is a bit of an invalid combo...
+				return CacheMode.REFRESH;
+			}
+			case BYPASS: {
+				return get ? CacheMode.GET : CacheMode.IGNORE;
+			}
+			default: {
+				throw new IllegalStateException( "huh? :)" );
+			}
+		}
+	}
+
+	public static CacheStoreMode interpretCacheStoreMode(CacheMode cacheMode) {
+		if ( cacheMode == null ) {
+			cacheMode = DEFAULT_LEGACY_MODE;
+		}
+
+		if ( CacheMode.REFRESH == cacheMode ) {
+			return CacheStoreMode.REFRESH;
+		}
+		if ( CacheMode.NORMAL == cacheMode || CacheMode.PUT == cacheMode ) {
+			return CacheStoreMode.USE;
+		}
+		return CacheStoreMode.BYPASS;
+	}
+
+	public static CacheRetrieveMode interpretCacheRetrieveMode(CacheMode cacheMode) {
+		if ( cacheMode == null ) {
+			cacheMode = DEFAULT_LEGACY_MODE;
+		}
+
+		return ( CacheMode.NORMAL == cacheMode || CacheMode.GET == cacheMode )
+				? CacheRetrieveMode.USE
+				: CacheRetrieveMode.BYPASS;
+	}
+}

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/cachemodes/SharedCacheModesTest.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/cachemodes/SharedCacheModesTest.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/cachemodes/SharedCacheModesTest.java	2010-01-22 01:55:20 UTC (rev 18603)
@@ -0,0 +1,120 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.ejb.test.cacheable.cachemodes;
+
+import javax.persistence.CacheRetrieveMode;
+import javax.persistence.CacheStoreMode;
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.hibernate.CacheMode;
+import org.hibernate.Session;
+import org.hibernate.ejb.AvailableSettings;
+import org.hibernate.ejb.HibernateEntityManager;
+import org.hibernate.ejb.HibernateQuery;
+import org.hibernate.ejb.test.TestCase;
+import org.hibernate.impl.AbstractQueryImpl;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class SharedCacheModesTest extends TestCase {
+	@Override
+	public Class[] getAnnotatedClasses() {
+		return new Class[] { SimpleEntity.class };
+	}
+
+	// this is all i can do for now testing-wise since I did not implement @Cacheable first ;)
+
+	public void testEntityManagerCacheModes() {
+
+		EntityManager em;
+		Session session;
+
+		em = getOrCreateEntityManager();
+		session = ( (HibernateEntityManager) em ).getSession();
+
+		// defaults...
+		assertEquals( CacheStoreMode.USE, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheRetrieveMode.USE, em.getProperties().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
+		assertEquals( CacheMode.NORMAL, session.getCacheMode() );
+
+		// overrides...
+		em.setProperty( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.REFRESH );
+		assertEquals( CacheStoreMode.REFRESH, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.REFRESH, session.getCacheMode() );
+
+		em.setProperty( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.BYPASS );
+		assertEquals( CacheStoreMode.BYPASS, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.GET, session.getCacheMode() );
+
+		em.setProperty( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS );
+		assertEquals( CacheRetrieveMode.BYPASS, em.getProperties().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
+		assertEquals( CacheMode.IGNORE, session.getCacheMode() );
+
+		em.setProperty( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.USE );
+		assertEquals( CacheStoreMode.USE, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.PUT, session.getCacheMode() );
+
+		em.setProperty( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.REFRESH );
+		assertEquals( CacheStoreMode.REFRESH, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.REFRESH, session.getCacheMode() );
+	}
+
+	public void testQueryCacheModes() {
+		EntityManager em = getOrCreateEntityManager();
+		Query jpaQuery = em.createQuery( "from SimpleEntity" );
+		AbstractQueryImpl hibQuery = (AbstractQueryImpl) ( (HibernateQuery) jpaQuery ).getHibernateQuery();
+
+		jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.USE );
+		assertEquals( CacheStoreMode.USE, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.NORMAL, hibQuery.getCacheMode() );
+
+		jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.BYPASS );
+		assertEquals( CacheStoreMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.GET, hibQuery.getCacheMode() );
+
+		jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.REFRESH );
+		assertEquals( CacheStoreMode.REFRESH, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.REFRESH, hibQuery.getCacheMode() );
+
+		jpaQuery.setHint( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS );
+		assertEquals( CacheRetrieveMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
+		assertEquals( CacheStoreMode.REFRESH, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.REFRESH, hibQuery.getCacheMode() );
+
+		jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.BYPASS );
+		assertEquals( CacheRetrieveMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
+		assertEquals( CacheStoreMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.IGNORE, hibQuery.getCacheMode() );
+
+		jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.USE );
+		assertEquals( CacheRetrieveMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
+		assertEquals( CacheStoreMode.USE, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
+		assertEquals( CacheMode.PUT, hibQuery.getCacheMode() );
+	}
+
+}

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/cachemodes/SimpleEntity.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/cachemodes/SimpleEntity.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/cacheable/cachemodes/SimpleEntity.java	2010-01-22 01:55:20 UTC (rev 18603)
@@ -0,0 +1,56 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.ejb.test.cacheable.cachemodes;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+ at Entity
+public class SimpleEntity {
+	private Long id;
+	private String name;
+
+	@Id
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+}



More information about the hibernate-commits mailing list