From affa6a086c1210b55433b84313b5da46b6218bbc Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Fri, 29 Apr 2016 14:48:26 -0500 Subject: [PATCH] Fix FlushMode --- .../org/hibernate/internal/SessionFactoryImpl.java | 14 +++++++++++--- .../java/org/hibernate/internal/SessionImpl.java | 11 ++++++----- .../query/internal/AbstractProducedQuery.java | 8 ++++---- .../org/hibernate/jpa/test/EntityManagerTest.java | 7 +++---- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 7dafca8e85..f148f9373c 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -150,6 +150,7 @@ import static org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting.det * * @author Gavin King * @author Steve Ebersole + * @author Chris Cranford */ public final class SessionFactoryImpl implements SessionFactoryImplementor { private static final CoreMessageLogger LOG = CoreLogging.messageLogger( SessionFactoryImpl.class ); @@ -215,7 +216,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { this.name = sfName; try { - uuid = (String) UUID_GENERATOR.generate(null, null); + uuid = (String) UUID_GENERATOR.generate( null, null ); } catch (Exception e) { throw new AssertionFailure("Could not generate UUID"); @@ -349,7 +350,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { // then construct the fetch instance... fetchProfile.addFetch( new Association( owner, mappingFetch.getAssociation() ), fetchStyle ); - ((Loadable) owner).registerAffectingFetchProfile( fetchProfile.getName() ); + ( (Loadable) owner ).registerAffectingFetchProfile( fetchProfile.getName() ); } fetchProfiles.put( fetchProfile.getName(), fetchProfile ); } @@ -551,7 +552,14 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { else { builder.autoJoinTransactions( false ); } - return builder.openSession(); + + final Session session = builder.openSession(); + map.keySet().forEach ( key -> { + if ( key instanceof String ) { + session.setProperty( (String) key, map.get( key ) ); + } + }); + return session; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 2900f8a852..d50b927d6e 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -211,6 +211,7 @@ import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_STORE_MODE; * @author Gavin King * @author Steve Ebersole * @author Brett Meyer + * @author Chris Cranford */ public final class SessionImpl extends AbstractSessionImpl @@ -294,7 +295,7 @@ public final class SessionImpl } private void setDefaultProperties() { - properties.putIfAbsent( AvailableSettings.FLUSH_MODE, getHibernateFlushMode() ); + properties.putIfAbsent( AvailableSettings.FLUSH_MODE, getHibernateFlushMode().name() ); properties.putIfAbsent( JPA_LOCK_SCOPE, PessimisticLockScope.EXTENDED.name() ); properties.putIfAbsent( JPA_LOCK_TIMEOUT, LockOptions.WAIT_FOREVER ); properties.putIfAbsent( JPA_SHARED_CACHE_RETRIEVE_MODE, CacheModeHelper.DEFAULT_RETRIEVE_MODE ); @@ -315,10 +316,10 @@ public final class SessionImpl protected void applyQuerySettingsAndHints(Query query) { if ( lockOptions.getLockMode() != LockMode.NONE ) { - query.setLockMode( getLockMode(lockOptions.getLockMode())); + query.setLockMode( getLockMode( lockOptions.getLockMode() ) ); } Object queryTimeout; - if ( (queryTimeout = getProperties().get(QueryHints.SPEC_HINT_TIMEOUT)) != null ) { + if ( (queryTimeout = getProperties().get( QueryHints.SPEC_HINT_TIMEOUT ) ) != null ) { query.setHint( QueryHints.SPEC_HINT_TIMEOUT, queryTimeout ); } Object lockTimeout; @@ -3172,7 +3173,7 @@ public final class SessionImpl } catch ( Exception ne ) { //we do not want the subsequent exception to swallow the original one - log.unableToMarkForRollbackOnPersistenceException(ne); + log.unableToMarkForRollbackOnPersistenceException( ne ); } } @@ -3687,7 +3688,7 @@ public final class SessionImpl applyProperties(); } else { - log.debugf("Trying to set a property which is not supported on entity manager level"); + log.debugf( "Trying to set a property which is not supported on entity manager level" ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java b/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java index 4a1d0d6219..b4aa9798ad 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java @@ -31,12 +31,10 @@ import org.hibernate.FlushMode; import org.hibernate.HibernateException; import org.hibernate.LockMode; import org.hibernate.LockOptions; -import org.hibernate.MappingException; import org.hibernate.NonUniqueResultException; import org.hibernate.PropertyNotFoundException; import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; -import org.hibernate.Session; import org.hibernate.TypeMismatchException; import org.hibernate.engine.query.spi.EntityGraphQueryHint; import org.hibernate.engine.query.spi.HQLQueryPlan; @@ -63,7 +61,6 @@ import org.hibernate.query.QueryParameter; import org.hibernate.query.spi.QueryImplementor; import org.hibernate.query.spi.QueryParameterBinding; import org.hibernate.transform.ResultTransformer; -import org.hibernate.type.SerializableType; import org.hibernate.type.Type; import static org.hibernate.jpa.QueryHints.HINT_CACHEABLE; @@ -144,7 +141,10 @@ public abstract class AbstractProducedQuery implements QueryImplementor { @Override public FlushModeType getFlushMode() { - return FlushModeTypeHelper.getFlushModeType( flushMode ); + return ( flushMode == null ? + getProducer().getFlushMode() : + FlushModeTypeHelper.getFlushModeType( flushMode ) + ); } @Override diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java index 18b3e21a8b..5bd5f7358c 100755 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java @@ -26,12 +26,11 @@ import javax.persistence.Query; import org.hibernate.FlushMode; import org.hibernate.HibernateException; +import org.hibernate.Session; import org.hibernate.cfg.Environment; import org.hibernate.jpa.AvailableSettings; -import org.hibernate.jpa.HibernateEntityManager; import org.hibernate.jpa.HibernateEntityManagerFactory; import org.hibernate.stat.Statistics; - import org.hibernate.testing.TestForIssue; import org.junit.Test; @@ -221,8 +220,8 @@ public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase { EntityManager em = getOrCreateEntityManager(); em.setFlushMode( FlushModeType.COMMIT ); assertEquals( FlushModeType.COMMIT, em.getFlushMode() ); - ( ( HibernateEntityManager ) em ).getSession().setFlushMode( FlushMode.ALWAYS ); - assertNull( em.getFlushMode() ); + ( (Session) em ).setFlushMode( FlushMode.ALWAYS ); + assertEquals( em.getFlushMode(), FlushModeType.AUTO ); em.close(); }