Author: hardy.ferentschik
Date: 2010-01-19 17:16:05 -0500 (Tue, 19 Jan 2010)
New Revision: 18585
Modified:
core/trunk/core/src/main/java/org/hibernate/LockOptions.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/util/ConfigurationHelper.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/EntityManagerTest.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/emops/FlushModeTest.java
Log:
HHH-4651 - Add support for EntityManager properties
Modified: core/trunk/core/src/main/java/org/hibernate/LockOptions.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/LockOptions.java 2010-01-19 21:52:23 UTC
(rev 18584)
+++ core/trunk/core/src/main/java/org/hibernate/LockOptions.java 2010-01-19 22:16:05 UTC
(rev 18585)
@@ -1,3 +1,4 @@
+// $Id:$
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
@@ -23,6 +24,7 @@
*/
package org.hibernate;
+import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
@@ -33,7 +35,7 @@
*
* @author Scott Marlow
*/
-public class LockOptions
+public class LockOptions implements Serializable
{
/**
* NO_WAIT timeout value will not block for pessimistic locking
@@ -67,10 +69,8 @@
private Map /* <String, LockMode> */ lockModesByName = new HashMap();
public LockOptions() {
-
}
-
public LockOptions( LockMode lockMode) {
this.lockMode = lockMode;
}
@@ -193,5 +193,4 @@
dest.lockModesByName = new HashMap(from.lockModesByName);
return dest;
}
-
}
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-19
21:52:23 UTC (rev 18584)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2010-01-19
22:16:05 UTC (rev 18585)
@@ -1,10 +1,11 @@
+// $Id:$
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
- * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
- * third-party contributors as indicated by either @author tags or express
- * copyright attribution statements applied by the authors. All
- * third-party contributions are distributed under license by Red Hat Inc.
+ * 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
@@ -27,28 +28,31 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityNotFoundException;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
+import javax.persistence.LockTimeoutException;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceContextType;
import javax.persistence.PersistenceException;
import javax.persistence.PessimisticLockException;
+import javax.persistence.PessimisticLockScope;
import javax.persistence.Query;
+import javax.persistence.QueryTimeoutException;
import javax.persistence.TransactionRequiredException;
import javax.persistence.Tuple;
import javax.persistence.TupleElement;
import javax.persistence.TypedQuery;
-import javax.persistence.PessimisticLockScope;
-import javax.persistence.LockTimeoutException;
-import javax.persistence.QueryTimeoutException;
+import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
-import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.spi.PersistenceUnitTransactionType;
@@ -61,36 +65,59 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.hibernate.*;
+import org.hibernate.AssertionFailure;
+import org.hibernate.FlushMode;
+import org.hibernate.HibernateException;
+import org.hibernate.LockMode;
+import org.hibernate.LockOptions;
+import org.hibernate.MappingException;
+import org.hibernate.ObjectDeletedException;
+import org.hibernate.ObjectNotFoundException;
+import org.hibernate.QueryException;
+import org.hibernate.SQLQuery;
+import org.hibernate.Session;
+import org.hibernate.StaleObjectStateException;
+import org.hibernate.StaleStateException;
+import org.hibernate.Transaction;
+import org.hibernate.TransientObjectException;
+import org.hibernate.TypeMismatchException;
+import org.hibernate.UnresolvableObjectException;
import org.hibernate.cfg.Environment;
+import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.ejb.criteria.ValueHandlerFactory;
import org.hibernate.ejb.criteria.expression.CompoundSelectionImpl;
import org.hibernate.ejb.transaction.JoinableCMTTransaction;
import org.hibernate.ejb.util.ConfigurationHelper;
-import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.transaction.TransactionFactory;
import org.hibernate.transform.BasicTransformerAdapter;
-import org.hibernate.util.CollectionHelper;
import org.hibernate.util.JTAHelper;
/**
* @author <a href="mailto:gavin@hibernate.org">Gavin King</a>
* @author Emmanuel Bernard
* @author Steve Ebersole
+ * @author Hardy Ferentschik
*/
@SuppressWarnings("unchecked")
public abstract class AbstractEntityManagerImpl implements
HibernateEntityManagerImplementor, Serializable {
private static final Logger log = LoggerFactory.getLogger(
AbstractEntityManagerImpl.class );
+ private static final List<String> entityManagerSpecificProperties = new
ArrayList<String>();
+ static {
+ entityManagerSpecificProperties.add( AvailableSettings.LOCK_SCOPE );
+ entityManagerSpecificProperties.add( AvailableSettings.LOCK_TIMEOUT );
+ entityManagerSpecificProperties.add( AvailableSettings.FLUSH_MODE );
+ }
+
private EntityManagerFactoryImpl entityManagerFactory;
protected transient TransactionImpl tx = new TransactionImpl( this );
protected PersistenceContextType persistenceContextType;
- private FlushModeType flushModeType = FlushModeType.AUTO;
private PersistenceUnitTransactionType transactionType;
- private Map properties;
+ private Map<String, Object> properties;
+ private LockOptions lockOptions;
protected AbstractEntityManagerImpl(
EntityManagerFactoryImpl entityManagerFactory,
@@ -100,7 +127,16 @@
this.entityManagerFactory = entityManagerFactory;
this.persistenceContextType = type;
this.transactionType = transactionType;
- this.properties = properties != null ? properties : CollectionHelper.EMPTY_MAP;
+
+ this.lockOptions = new LockOptions( );
+ this.properties = new HashMap<String, Object>();
+ if(properties != null) {
+ for ( String key : entityManagerSpecificProperties ) {
+ if ( properties.containsKey( key ) ) {
+ this.properties.put( key, properties.get( key ) );
+ }
+ }
+ }
}
protected void postInit() {
@@ -108,13 +144,67 @@
if ( PersistenceUnitTransactionType.JTA.equals( transactionType ) ) {
joinTransaction( true );
}
- Object flushMode = properties.get( "org.hibernate.flushMode" );
- if ( flushMode != null ) {
- getSession().setFlushMode( ConfigurationHelper.getFlushMode( flushMode ) );
+
+ setDefaultProperties();
+ applyProperties();
+ }
+
+ private void applyProperties() {
+ getSession().setFlushMode( ConfigurationHelper.getFlushMode( properties.get(
AvailableSettings.FLUSH_MODE ) ) );
+ setLockOptions(this.properties, this.lockOptions);
+ }
+
+ 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 ) {
+ options.setScope( true );
}
- this.properties = null;
+ else if ( lockScope instanceof PessimisticLockScope ) {
+ boolean extended = PessimisticLockScope.EXTENDED.equals( ( PessimisticLockScope )
lockScope );
+ options.setScope( extended );
+ }
+ else {
+ throw new PersistenceException( "Unable to parse " +
AvailableSettings.LOCK_SCOPE + ": " + lockScope );
+ }
+
+ Object lockTimeout = props.get( AvailableSettings.LOCK_TIMEOUT );
+ int timeout;
+ if ( lockTimeout instanceof String ) {
+ timeout = Integer.parseInt( ( String ) lockTimeout );
+ }
+ else if ( lockTimeout instanceof Integer ) {
+ timeout = ( Integer ) lockTimeout;
+ }
+ else {
+ throw new PersistenceException( "Unable to parse " +
AvailableSettings.LOCK_TIMEOUT + ": " + lockTimeout );
+ }
+ if ( timeout < 0 ) {
+ options.setTimeOut( LockOptions.WAIT_FOREVER );
+ }
+ else if ( timeout == 0 ) {
+ options.setTimeOut( LockOptions.NO_WAIT );
+ }
+ else {
+ options.setTimeOut( timeout );
+ }
}
+ /**
+ * Sets the default property values for the properties the entity manager supports and
which are not already explicitly
+ * set.
+ */
+ private void setDefaultProperties() {
+ if (properties.get( AvailableSettings.FLUSH_MODE ) == null) {
+ properties.put( AvailableSettings.FLUSH_MODE, getSession().getFlushMode().toString()
);
+ }
+ if (properties.get( AvailableSettings.LOCK_SCOPE ) == null) {
+ this.properties.put( AvailableSettings.LOCK_SCOPE,
PessimisticLockScope.EXTENDED.name() );
+ }
+ if (properties.get( AvailableSettings.LOCK_TIMEOUT ) == null) {
+ properties.put( AvailableSettings.LOCK_TIMEOUT, LockOptions.WAIT_FOREVER );
+ }
+ }
+
public Query createQuery(String jpaqlString) {
try {
return new QueryImpl<Object>( getSession().createQuery( jpaqlString ), this );
@@ -328,7 +418,6 @@
}
}
-
public Query createNativeQuery(String sqlString) {
try {
SQLQuery q = getSession().createSQLQuery( sqlString );
@@ -397,9 +486,12 @@
public <A> A find(Class<A> entityClass, Object primaryKey, LockModeType
lockModeType, Map<String, Object> properties) {
LockOptions lockOptions = null;
try {
- if ( lockModeType != null )
- return ( A ) getSession().get( entityClass, ( Serializable ) primaryKey,
- ( lockOptions = getLockRequest(lockModeType, properties) ) );
+ if ( lockModeType != null ) {
+ return ( A ) getSession().get(
+ entityClass, ( Serializable ) primaryKey,
+ getLockRequest( lockModeType, properties )
+ );
+ }
else
return ( A ) getSession().get( entityClass, ( Serializable ) primaryKey );
}
@@ -530,17 +622,21 @@
if ( !contains( entity ) ) {
throw new IllegalArgumentException( "entity not in the persistence context"
);
}
- return this.getLockModeType(getSession().getCurrentLockMode(entity));
+ return getLockModeType(getSession().getCurrentLockMode(entity));
}
public void setProperty(String s, Object o) {
- //FIXME
- //To change body of implemented methods use File | Settings | File Templates.
+ if ( entityManagerSpecificProperties.contains( s ) ) {
+ properties.put( s, o );
+ applyProperties();
+ }
+ else {
+ log.debug( "Trying to set a property which is not supported on entity manager
level" );
+ }
}
public Map<String, Object> getProperties() {
- //FIXME
- return null; //To change body of implemented methods use File | Settings | File
Templates.
+ return Collections.unmodifiableMap( properties );
}
public void flush() {
@@ -548,7 +644,6 @@
if ( !isTransactionInProgress() ) {
throw new TransactionRequiredException( "no transaction is in progress" );
}
- //adjustFlushMode();
getSession().flush();
}
catch ( RuntimeException e ) {
@@ -606,7 +701,6 @@
}
public void setFlushMode(FlushModeType flushModeType) {
- this.flushModeType = flushModeType;
if ( flushModeType == FlushModeType.AUTO ) {
getSession().setFlushMode( FlushMode.AUTO );
}
@@ -619,7 +713,6 @@
}
public void clear() {
- //adjustFlushMode();
try {
getSession().clear();
}
@@ -646,16 +739,15 @@
public FlushModeType getFlushMode() {
FlushMode mode = getSession().getFlushMode();
if ( mode == FlushMode.AUTO ) {
- this.flushModeType = FlushModeType.AUTO;
+ return FlushModeType.AUTO;
}
else if ( mode == FlushMode.COMMIT ) {
- this.flushModeType = FlushModeType.COMMIT;
+ return FlushModeType.COMMIT;
}
else {
+ // otherwise this is an unknown mode for EJB3
return null;
}
- //otherwise this is an unknown mode for EJB3
- return flushModeType;
}
public void lock(Object entity, LockModeType lockMode) {
@@ -668,7 +760,6 @@
if ( !isTransactionInProgress() ) {
throw new TransactionRequiredException( "no transaction is in progress" );
}
- //adjustFlushMode();
if ( !contains( entity ) ) {
throw new IllegalArgumentException( "entity not in the persistence context"
);
}
@@ -677,36 +768,19 @@
catch ( HibernateException he ) {
throw convert( he , lockOptions);
}
-
}
public LockOptions getLockRequest(LockModeType lockModeType, Map<String, Object>
properties) {
LockOptions lockOptions = new LockOptions();
- lockOptions.setLockMode(getLockMode(lockModeType));
+ LockOptions.copy( this.lockOptions, lockOptions );
+ lockOptions.setLockMode( getLockMode( lockModeType ) );
if ( properties != null ) {
- // lockOptions scope will default to false (PessimisticLockScope.NORMAL)
- Object value = properties.get( AvailableSettings.LOCK_SCOPE );
- if ( value instanceof String && PessimisticLockScope.valueOf((String) value)
== PessimisticLockScope.EXTENDED) {
- lockOptions.setScope(true);
- }
- // lockOptions timeout will default to LockOptions.FOREVER_WAIT
- value = properties.get( AvailableSettings.LOCK_TIMEOUT );
- if ( value instanceof String ) {
- int timeout = Integer.parseInt((String) value);
- if ( timeout < 0 ) {
- lockOptions.setTimeOut(LockOptions.WAIT_FOREVER);
- }
- else if( timeout == 0 ) {
- lockOptions.setTimeOut(LockOptions.NO_WAIT);
- }
- else {
- lockOptions.setTimeOut(timeout);
- }
- }
+ setLockOptions( properties, lockOptions );
}
return lockOptions;
}
+ @SuppressWarnings("deprecation")
private static LockModeType getLockModeType(LockMode lockMode) {
if ( lockMode == LockMode.NONE )
return LockModeType.NONE;
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-19
21:52:23 UTC (rev 18584)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java 2010-01-19
22:16:05 UTC (rev 18585)
@@ -168,65 +168,83 @@
* JAR autodetection artifacts class, hbm
*/
public static final String AUTODETECTION = "hibernate.archive.autodetection";
+
/**
* cfg.xml configuration file used
*/
public static final String CFG_FILE = "hibernate.ejb.cfgfile";
+
/**
* Caching configuration should follow the following pattern
* hibernate.ejb.classcache.<fully.qualified.Classname> usage[, region]
* where usage is the cache strategy used and region the cache region name
*/
public static final String CLASS_CACHE_PREFIX = "hibernate.ejb.classcache";
+
/**
* Caching configuration should follow the following pattern
* hibernate.ejb.collectioncache.<fully.qualified.Classname>.<role> usage[,
region]
* where usage is the cache strategy used and region the cache region name
*/
public static final String COLLECTION_CACHE_PREFIX =
"hibernate.ejb.collectioncache";
+
/**
* Interceptor class name, the class has to have a no-arg constructor
* the interceptor instance is shared amongst all EntityManager of a given
EntityManagerFactory
*/
public static final String INTERCEPTOR = "hibernate.ejb.interceptor";
+
/**
* Interceptor class name, the class has to have a no-arg constructor
*/
public static final String SESSION_INTERCEPTOR =
"hibernate.ejb.interceptor.session_scoped";
+
/**
* Naming strategy class name, the class has to have a no-arg constructor
*/
public static final String NAMING_STRATEGY = "hibernate.ejb.naming_strategy";
+
/**
* Event configuration should follow the following pattern
* hibernate.ejb.event.[eventType] f.q.c.n.EventListener1, f.q.c.n.EventListener12 ...
*/
public static final String EVENT_LISTENER_PREFIX = "hibernate.ejb.event";
+
/**
* Enable the class file enhancement
*/
public static final String USE_CLASS_ENHANCER =
"hibernate.ejb.use_class_enhancer";
+
/**
* Whether or not discard persistent context on entityManager.close()
* The EJB3 compliant and default choice is false
*/
public static final String DISCARD_PC_ON_CLOSE =
"hibernate.ejb.discard_pc_on_close";
+
/**
* Consider this as experimental
* It is not recommended to set up this property, the configuration is stored
* in the JNDI in a serialized form
*/
public static final String CONFIGURATION_JNDI_NAME =
"hibernate.ejb.configuration_jndi_name";
+
/**
+ * Used to detrmine flush mode.
+ */
+ public static final String FLUSH_MODE = "org.hibernate.flushMode";
+
+ /**
* List of classes names
* Internal use only
*/
public static final String CLASS_NAMES = "hibernate.ejb.classes";
+
/**
* List of annotated packages
* Internal use only
*/
public static final String PACKAGE_NAMES = "hibernate.ejb.packages";
+
/**
* List of classes names
* Internal use only
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/util/ConfigurationHelper.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/util/ConfigurationHelper.java 2010-01-19
21:52:23 UTC (rev 18584)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/util/ConfigurationHelper.java 2010-01-19
22:16:05 UTC (rev 18585)
@@ -78,7 +78,6 @@
default:
throw new AssertionFailure("Unknown FlushModeType: " + flushMode);
}
-
}
public static Integer getInteger(Object value) {
Modified:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/EntityManagerTest.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/EntityManagerTest.java 2010-01-19
21:52:23 UTC (rev 18584)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/EntityManagerTest.java 2010-01-19
22:16:05 UTC (rev 18585)
@@ -1,4 +1,27 @@
//$Id$
+/*
+ * 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;
import java.io.ByteArrayInputStream;
@@ -10,11 +33,13 @@
import java.util.HashSet;
import java.util.Map;
import javax.persistence.EntityManager;
+import javax.persistence.EntityNotFoundException;
import javax.persistence.FlushModeType;
import javax.persistence.Query;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
+import org.hibernate.ejb.AvailableSettings;
import org.hibernate.ejb.HibernateEntityManager;
import org.hibernate.ejb.HibernateEntityManagerFactory;
import org.hibernate.stat.Statistics;
@@ -25,7 +50,7 @@
public class EntityManagerTest extends TestCase {
public Class[] getAnnotatedClasses() {
- return new Class[]{
+ return new Class[] {
Item.class,
Distributor.class,
Wallet.class
@@ -44,55 +69,8 @@
return result;
}
- // EM TRANSACTION
-// public void testEntityManager() {
-//
-// Item item = new Item( "Mouse", "Micro$oft mouse" );
-//
-// EntityManager em = getEntityManager();
-// em.getTransaction().begin();
-// em.persist( item );
-// assertTrue( em.contains( item ) );
-// em.getTransaction().commit();
-//
-// assertFalse( em.contains( item ) );
-//
-// em.getTransaction().begin();
-// item = (Item) em.createQuery( "from Item where descr like 'M%'"
).getSingleResult();
-// assertNotNull( item );
-// assertEquals( "Micro$oft mouse", item.getDescr() );
-// item.setDescr( "Micro$oft wireless mouse" );
-// assertTrue( em.contains( item ) );
-// em.getTransaction().commit();
-//
-// assertFalse( em.contains( item ) );
-//
-// em.getTransaction().begin();
-// item = (Item) em.find( Item.class, "Mouse" );
-// assertNotNull( item );
-// em.getTransaction().commit();
-//
-// item = em.find( Item.class, "Mouse" );
-// assertFalse( em.contains( item ) );
-//
-// item = (Item) em.createQuery( "from Item where descr like 'M%'"
).getSingleResult();
-// assertNotNull( item );
-// assertFalse( em.contains( item ) );
-//
-// em.getTransaction().begin();
-// item = em.find( Item.class, "Mouse" );
-// assertNotNull( item );
-// assertTrue( em.contains( item ) );
-// assertEquals( "Micro$oft wireless mouse", item.getDescr() );
-// em.remove( item );
-// em.remove( item );//second should be a no-op
-// em.getTransaction().commit();
-//
-// em.close();
-// }
+ public void testEntityManager() {
- public void testExtendedEntityManager() {
-
Item item = new Item( "Mouse", "Micro$oft mouse" );
EntityManager em = getOrCreateEntityManager();
@@ -104,7 +82,7 @@
assertTrue( em.contains( item ) );
em.getTransaction().begin();
- Item item1 = (Item) em.createQuery( "select i from Item i where descr like
'M%'" ).getSingleResult();
+ Item item1 = ( Item ) em.createQuery( "select i from Item i where descr like
'M%'" ).getSingleResult();
assertNotNull( item1 );
assertSame( item, item1 );
item.setDescr( "Micro$oft wireless mouse" );
@@ -123,7 +101,7 @@
assertSame( item, item1 );
assertTrue( em.contains( item ) );
- item1 = (Item) em.createQuery( "select i from Item i where descr like
'M%'" ).getSingleResult();
+ item1 = ( Item ) em.createQuery( "select i from Item i where descr like
'M%'" ).getSingleResult();
assertNotNull( item1 );
assertSame( item, item1 );
assertTrue( em.contains( item ) );
@@ -144,7 +122,7 @@
res.setName( "Bruce" );
item.setDistributors( new HashSet<Distributor>() );
item.getDistributors().add( res );
- Statistics stats = ( (HibernateEntityManagerFactory) factory
).getSessionFactory().getStatistics();
+ Statistics stats = ( ( HibernateEntityManagerFactory ) factory
).getSessionFactory().getStatistics();
stats.clear();
stats.setStatisticsEnabled( true );
@@ -191,9 +169,11 @@
em.contains( nonManagedObject );
fail( "Should have raised an exception" );
}
- catch (IllegalArgumentException iae) {
+ catch ( IllegalArgumentException iae ) {
//success
- if ( em.getTransaction() != null ) em.getTransaction().rollback();
+ if ( em.getTransaction() != null ) {
+ em.getTransaction().rollback();
+ }
}
finally {
em.close();
@@ -232,7 +212,7 @@
EntityManager em = getOrCreateEntityManager();
em.setFlushMode( FlushModeType.COMMIT );
assertEquals( FlushModeType.COMMIT, em.getFlushMode() );
- ( (HibernateEntityManager) em ).getSession().setFlushMode( FlushMode.ALWAYS );
+ ( ( HibernateEntityManager ) em ).getSession().setFlushMode( FlushMode.ALWAYS );
assertNull( em.getFlushMode() );
em.close();
}
@@ -261,7 +241,7 @@
Query query = em.createQuery( "SELECT p FETCH JOIN p.distributors FROM Item
p" );
query.getSingleResult();
}
- catch (IllegalArgumentException e) {
+ catch ( IllegalArgumentException e ) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream( stream );
out.writeObject( e );
@@ -270,7 +250,7 @@
stream.close();
ByteArrayInputStream byteIn = new ByteArrayInputStream( serialized );
ObjectInputStream in = new ObjectInputStream( byteIn );
- IllegalArgumentException deserializedException = (IllegalArgumentException)
in.readObject();
+ IllegalArgumentException deserializedException = ( IllegalArgumentException )
in.readObject();
in.close();
byteIn.close();
assertNull( deserializedException.getCause().getCause() );
@@ -289,7 +269,7 @@
stream.close();
ByteArrayInputStream byteIn = new ByteArrayInputStream( serialized );
ObjectInputStream in = new ObjectInputStream( byteIn );
- HibernateException deserializedException = (HibernateException) in.readObject();
+ HibernateException deserializedException = ( HibernateException ) in.readObject();
in.close();
byteIn.close();
assertNotNull( "Arbitrary exceptions nullified",
deserializedException.getCause() );
@@ -307,30 +287,78 @@
}
//EJB-9
-// public void testGet() throws Exception {
-// EntityManager em = getEntityManager();
-// em.getTransaction().begin();
-// Item item = (Item) em.get(Item.class, "nonexistentone");
-// try {
-// item.getDescr();
-// em.getTransaction().commit();
-// fail("Object with wrong id should have failed");
-// }
-// catch (EntityNotFoundException e) {
-// //success
-// if (em.getTransaction() != null) em.getTransaction().rollback();
-// }
-// finally {
-// em.close();
-// }
-// }
+ public void testGet() throws Exception {
+ EntityManager em = getOrCreateEntityManager();
+ em.getTransaction().begin();
+ Item item = ( Item ) em.getReference( Item.class, "nonexistentone" );
+ try {
+ item.getDescr();
+ em.getTransaction().commit();
+ fail( "Object with wrong id should have failed" );
+ }
+ catch ( EntityNotFoundException e ) {
+ //success
+ if ( em.getTransaction() != null ) {
+ em.getTransaction().rollback();
+ }
+ }
+ finally {
+ em.close();
+ }
+ }
- public EntityManagerTest() {
- super();
+ public void testGetProperties() throws Exception {
+ EntityManager em = getOrCreateEntityManager();
+ Map<String, Object> properties = em.getProperties();
+ assertNotNull( properties );
+ try {
+ properties.put( "foo", "bar" );
+ fail();
+ }
+ catch ( UnsupportedOperationException e ) {
+ // success
+ }
+
+ assertTrue( properties.containsKey( AvailableSettings.FLUSH_MODE ) );
}
- public EntityManagerTest(String arg0) {
- super( arg0 );
+ public void testSetProperty() throws Exception {
+ EntityManager em = getOrCreateEntityManager();
+ em.getTransaction().begin();
+ Wallet wallet = new Wallet();
+ wallet.setSerial( "000" );
+ em.persist( wallet );
+ em.getTransaction().commit();
+
+ em.clear();
+ assertEquals( em.getProperties().get( AvailableSettings.FLUSH_MODE ), "AUTO"
);
+ assertNotNull(
+ "With default settings the entity should be persisted on commit.",
+ em.find( Wallet.class, wallet.getSerial() )
+ );
+
+ em.getTransaction().begin();
+ wallet = em.merge( wallet );
+ em.remove( wallet );
+ em.getTransaction().commit();
+
+ em.clear();
+ assertNull( "The entity should have been removed.", em.find( Wallet.class,
wallet.getSerial() ) );
+
+ em.setProperty( "org.hibernate.flushMode", "MANUAL" +
+ "" );
+ em.getTransaction().begin();
+ wallet = new Wallet();
+ wallet.setSerial( "000" );
+ em.persist( wallet );
+ em.getTransaction().commit();
+
+ em.clear();
+ assertNull(
+ "With a flush mode of manual the entity should not have been persisted.",
+ em.find( Wallet.class, wallet.getSerial() )
+ );
+ assertEquals( "MANUAL", em.getProperties().get( AvailableSettings.FLUSH_MODE
) );
+ em.close();
}
-
}
Modified:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/emops/FlushModeTest.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/emops/FlushModeTest.java 2010-01-19
21:52:23 UTC (rev 18584)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/emops/FlushModeTest.java 2010-01-19
22:16:05 UTC (rev 18585)
@@ -1,8 +1,31 @@
//$Id$
+/*
+ * 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.emops;
+import java.util.HashMap;
import java.util.Map;
-import java.util.HashMap;
import javax.persistence.EntityManager;
import org.hibernate.ejb.test.TestCase;
@@ -13,12 +36,12 @@
public class FlushModeTest extends TestCase {
public void testCreateEMFlushMode() throws Exception {
- Map properties = new HashMap();
+ Map<String, String> properties = new HashMap<String, String>();
properties.put( "org.hibernate.flushMode", "manual" );
EntityManager em = createEntityManager( properties );
em.getTransaction().begin();
Dress dress = new Dress();
- dress.name = "long dress";
+ dress.name = "long dress";
em.persist( dress );
em.getTransaction().commit();