From: <hib...@li...> - 2006-07-10 18:47:09
|
Author: epbernard Date: 2006-07-10 14:46:58 -0400 (Mon, 10 Jul 2006) New Revision: 10103 Added: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Dress.java trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/FlushModeTest.java Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/CurrentEntityManagerImpl.java trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/EntityManagerFactoryImpl.java trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/EntityManagerImpl.java trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/util/ConfigurationHelper.java Log: EJB-184 add flush mode through createEntityManager() Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2006-07-10 18:31:16 UTC (rev 10102) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2006-07-10 18:46:58 UTC (rev 10103) @@ -8,6 +8,7 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.util.Map; import javax.persistence.EntityExistsException; import javax.persistence.EntityNotFoundException; import javax.persistence.EntityTransaction; @@ -44,12 +45,14 @@ import org.hibernate.QueryException; import org.hibernate.TransientObjectException; import org.hibernate.ejb.transaction.JoinableCMTTransaction; +import org.hibernate.ejb.util.ConfigurationHelper; import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.engine.SessionImplementor; import org.hibernate.exception.ConstraintViolationException; import org.hibernate.proxy.HibernateProxy; import org.hibernate.transaction.TransactionFactory; import org.hibernate.util.JTAHelper; +import org.hibernate.util.CollectionHelper; /** * @author <a href="mailto:ga...@hi...">Gavin King</a> @@ -60,17 +63,26 @@ protected transient TransactionImpl tx = new TransactionImpl( this ); protected PersistenceContextType persistenceContextType; - private FlushModeType flushMode = FlushModeType.AUTO; + private FlushModeType flushModeType = FlushModeType.AUTO; private PersistenceUnitTransactionType transactionType; + private Map properties; - protected AbstractEntityManagerImpl(PersistenceContextType type, PersistenceUnitTransactionType transactionType) { + protected AbstractEntityManagerImpl( + PersistenceContextType type, PersistenceUnitTransactionType transactionType, Map properties + ) { this.persistenceContextType = type; this.transactionType = transactionType; + this.properties = properties != null ? properties : CollectionHelper.EMPTY_MAP; } protected void postInit() { //register in Sync if needed if ( PersistenceUnitTransactionType.JTA.equals( transactionType ) ) joinTransaction( true ); + Object flushMode = properties.get( "org.hibernate.flushMode" ); + if (flushMode != null) { + getSession().setFlushMode( ConfigurationHelper.getFlushMode( flushMode ) ); + } + this.properties = null; } public Query createQuery(String ejbqlString) { @@ -296,16 +308,16 @@ return tx; } - public void setFlushMode(FlushModeType flushMode) { - this.flushMode = flushMode; - if ( flushMode == FlushModeType.AUTO ) { + public void setFlushMode(FlushModeType flushModeType) { + this.flushModeType = flushModeType; + if ( flushModeType == FlushModeType.AUTO ) { getSession().setFlushMode( FlushMode.AUTO ); } - else if ( flushMode == FlushModeType.COMMIT ) { + else if ( flushModeType == FlushModeType.COMMIT ) { getSession().setFlushMode( FlushMode.COMMIT ); } else { - throw new AssertionFailure( "Unknown FlushModeType: " + flushMode ); + throw new AssertionFailure( "Unknown FlushModeType: " + flushModeType ); } } @@ -322,10 +334,10 @@ public FlushModeType getFlushMode() { FlushMode mode = getSession().getFlushMode(); if ( mode == FlushMode.AUTO ) { - this.flushMode = FlushModeType.AUTO; + this.flushModeType = FlushModeType.AUTO; } else if ( mode == FlushMode.COMMIT ) { - this.flushMode = FlushModeType.COMMIT; + this.flushModeType = FlushModeType.COMMIT; } // else if ( mode == FlushMode.NEVER ) { // if ( PersistenceContextType.EXTENDED == persistenceContextType && !isTransactionInProgress() ) { @@ -340,7 +352,7 @@ return null; //TODO exception? } //otherwise this is an unknown mode for EJB3 - return flushMode; + return flushModeType; } public void lock(Object entity, LockModeType lockMode) { @@ -377,13 +389,13 @@ boolean isTransactionActive = isTransactionInProgress(); - if ( isTransactionActive && session.getFlushMode() == FlushMode.NEVER ) { - log.debug( "Transaction activated, move to FlushMode " + flushMode ); - setFlushMode( flushMode ); + if ( isTransactionActive && session.getFlushMode() == FlushMode.MANUAL ) { + log.debug( "Transaction activated, move to FlushMode " + flushModeType ); + setFlushMode( flushModeType ); } - else if ( ! isTransactionActive && session.getFlushMode() != FlushMode.NEVER ) { + else if ( ! isTransactionActive && session.getFlushMode() != FlushMode.MANUAL ) { log.debug( "Transaction not active, move to FlushMode NEVER" ); - session.setFlushMode( FlushMode.NEVER ); + session.setFlushMode( FlushMode.MANUAL ); } } @@ -585,7 +597,7 @@ } else if ( e instanceof TransientObjectException ) { markAsRollback(); - throw new IllegalStateException( e ); //Spec 3.2.3 Synchronozation rules + throw new IllegalStateException( e ); //Spec 3.2.3 Synchronization rules } else { throwPersistenceException( new PersistenceException( e ) ); Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/CurrentEntityManagerImpl.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/CurrentEntityManagerImpl.java 2006-07-10 18:31:16 UTC (rev 10102) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/CurrentEntityManagerImpl.java 2006-07-10 18:46:58 UTC (rev 10103) @@ -1,6 +1,7 @@ //$Id$ package org.hibernate.ejb; +import java.util.Map; import javax.persistence.PersistenceContextType; import javax.persistence.spi.PersistenceUnitTransactionType; @@ -19,8 +20,8 @@ private SessionFactory sessionFactory; - public CurrentEntityManagerImpl(SessionFactory sessionFactory, PersistenceUnitTransactionType transactionType) { - super( PersistenceContextType.TRANSACTION, transactionType ); + public CurrentEntityManagerImpl(SessionFactory sessionFactory, PersistenceUnitTransactionType transactionType, Map properties) { + super( PersistenceContextType.TRANSACTION, transactionType, properties ); this.sessionFactory = sessionFactory; postInit(); } Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/EntityManagerFactoryImpl.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/EntityManagerFactoryImpl.java 2006-07-10 18:31:16 UTC (rev 10102) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/EntityManagerFactoryImpl.java 2006-07-10 18:46:58 UTC (rev 10103) @@ -35,7 +35,7 @@ public EntityManager createEntityManager(Map map) { //TODO support discardOnClose, persistencecontexttype?, interceptor, return new EntityManagerImpl( - sessionFactory, PersistenceContextType.EXTENDED, transactionType, discardOnClose + sessionFactory, PersistenceContextType.EXTENDED, transactionType, discardOnClose, map ); } Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/EntityManagerImpl.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/EntityManagerImpl.java 2006-07-10 18:31:16 UTC (rev 10102) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/EntityManagerImpl.java 2006-07-10 18:46:58 UTC (rev 10103) @@ -1,6 +1,7 @@ //$Id$ package org.hibernate.ejb; +import java.util.Map; import javax.persistence.PersistenceContextType; import javax.persistence.spi.PersistenceUnitTransactionType; import javax.transaction.Synchronization; @@ -27,9 +28,9 @@ public EntityManagerImpl( SessionFactory sessionFactory, PersistenceContextType pcType, PersistenceUnitTransactionType transactionType, - boolean discardOnClose + boolean discardOnClose, Map properties ) { - super( pcType, transactionType ); + super( pcType, transactionType, properties ); this.sessionFactory = sessionFactory; this.open = true; this.discardOnClose = discardOnClose; Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java 2006-07-10 18:31:16 UTC (rev 10102) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java 2006-07-10 18:46:58 UTC (rev 10103) @@ -18,6 +18,7 @@ import org.hibernate.QueryParameterException; import org.hibernate.SQLQuery; import org.hibernate.TypeMismatchException; +import org.hibernate.ejb.util.ConfigurationHelper; import org.hibernate.hql.QueryExecutionRequestException; /** @@ -143,7 +144,7 @@ query.setCacheMode( (CacheMode) value ); } else if ( "org.hibernate.flushMode".equals( hintName ) ) { - query.setFlushMode( (FlushMode) value ); + query.setFlushMode( ConfigurationHelper.getFlushMode( value ) ); } //TODO: /*else if ( "org.hibernate.lockMode".equals( hintName ) ) { Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/util/ConfigurationHelper.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/util/ConfigurationHelper.java 2006-07-10 18:31:16 UTC (rev 10102) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/util/ConfigurationHelper.java 2006-07-10 18:46:58 UTC (rev 10103) @@ -5,6 +5,12 @@ import java.util.Properties; import java.util.Set; +import javax.persistence.FlushModeType; +import javax.persistence.PersistenceException; + +import org.hibernate.FlushMode; +import org.hibernate.AssertionFailure; + /** * @author Emmanuel Bernard */ @@ -16,4 +22,39 @@ } } } + + public static FlushMode getFlushMode(Object value) { + FlushMode flushMode = null; + if (value instanceof FlushMode) { + flushMode = (FlushMode) value; + } + else if (value instanceof javax.persistence.FlushModeType) { + flushMode = ConfigurationHelper.getFlushMode( (javax.persistence.FlushModeType) value); + } + else if (value instanceof String) { + flushMode = ConfigurationHelper.getFlushMode( (String) value); + } + if (flushMode == null) { + throw new PersistenceException("Unable to parse org.hibernate.flushMode: " + value); + } + return flushMode; + } + + private static FlushMode getFlushMode(String flushMode) { + if (flushMode == null) return null; + flushMode = flushMode.toUpperCase(); + return FlushMode.parse( flushMode ); + } + + private static FlushMode getFlushMode(FlushModeType flushMode) { + switch(flushMode) { + case AUTO: + return FlushMode.AUTO; + case COMMIT: + return FlushMode.COMMIT; + default: + throw new AssertionFailure("Unknown FlushModeType: " + flushMode); + } + + } } Added: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Dress.java =================================================================== --- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Dress.java 2006-07-10 18:31:16 UTC (rev 10102) +++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Dress.java 2006-07-10 18:46:58 UTC (rev 10103) @@ -0,0 +1,14 @@ +//$Id: $ +package org.hibernate.ejb.test.emops; + +import javax.persistence.Id; +import javax.persistence.Entity; + +/** + * @author Emmanuel Bernard + */ +@Entity +public class Dress { + @Id public String name; + +} Added: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/FlushModeTest.java =================================================================== --- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/FlushModeTest.java 2006-07-10 18:31:16 UTC (rev 10102) +++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/FlushModeTest.java 2006-07-10 18:46:58 UTC (rev 10103) @@ -0,0 +1,39 @@ +//$Id: $ +package org.hibernate.ejb.test.emops; + +import java.util.Map; +import java.util.HashMap; +import javax.persistence.EntityManager; + +import org.hibernate.ejb.test.TestCase; + +/** + * @author Emmanuel Bernard + */ +public class FlushModeTest extends TestCase { + + public void testCreateEMFlushMode() throws Exception { + Map properties = new HashMap(); + properties.put( "org.hibernate.flushMode", "manual" ); + EntityManager em = factory.createEntityManager( properties ); + em.getTransaction().begin(); + Dress dress = new Dress(); + dress.name = "long dress"; + em.persist( dress ); + em.getTransaction().commit(); + + em.clear(); + + assertNull( em.find( Dress.class, dress.name ) ); + + em.close(); + } + + public Class[] getAnnotatedClasses() { + return new Class[] { + Race.class, + Competitor.class, + Dress.class + }; + } +} |