Author: epbernard Date: 2006-08-08 13:14:16 -0400 (Tue, 08 Aug 2006) New Revision: 10233 Added: trunk/HibernateExt/ejb-api/src/javax/persistence/PersistenceProperty.java Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/AssociationOverride.java trunk/HibernateExt/ejb-api/src/javax/persistence/CascadeType.java trunk/HibernateExt/ejb-api/src/javax/persistence/DiscriminatorType.java trunk/HibernateExt/ejb-api/src/javax/persistence/EntityManager.java trunk/HibernateExt/ejb-api/src/javax/persistence/EntityManagerFactory.java trunk/HibernateExt/ejb-api/src/javax/persistence/EntityTransaction.java trunk/HibernateExt/ejb-api/src/javax/persistence/EnumType.java trunk/HibernateExt/ejb-api/src/javax/persistence/FetchType.java trunk/HibernateExt/ejb-api/src/javax/persistence/FlushModeType.java trunk/HibernateExt/ejb-api/src/javax/persistence/OptimisticLockException.java trunk/HibernateExt/ejb-api/src/javax/persistence/Persistence.java trunk/HibernateExt/ejb-api/src/javax/persistence/Query.java trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/exception/ExceptionTest.java Log: EJB-210 OptimisticLockException get the entity object EJB-211 enhance EJB3 javadoc Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -44,6 +44,7 @@ import org.hibernate.TypeMismatchException; import org.hibernate.QueryException; import org.hibernate.TransientObjectException; +import org.hibernate.StaleObjectStateException; import org.hibernate.ejb.transaction.JoinableCMTTransaction; import org.hibernate.ejb.util.ConfigurationHelper; import org.hibernate.engine.SessionFactoryImplementor; @@ -577,7 +578,14 @@ public void throwPersistenceException(HibernateException e) { if ( e instanceof StaleStateException ) { - throwPersistenceException( new OptimisticLockException( e ) ); + if ( e instanceof StaleObjectStateException ) { + StaleObjectStateException sose = (StaleObjectStateException) e; + Object entity = getSession().load( sose.getEntityName(), sose.getIdentifier() ); + throwPersistenceException( new OptimisticLockException( null, e, entity ) ); + } + else { + throwPersistenceException( new OptimisticLockException( e ) ); + } } else if ( e instanceof ConstraintViolationException ) { //FIXME this is bad cause ConstraintViolationException happens in other circumstances Modified: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/exception/ExceptionTest.java =================================================================== --- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/exception/ExceptionTest.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/exception/ExceptionTest.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -1,10 +1,12 @@ //$Id: $ package org.hibernate.ejb.test.exception; +import java.util.Map; import javax.persistence.EntityManager; +import javax.persistence.EntityNotFoundException; import javax.persistence.OptimisticLockException; -import javax.persistence.EntityNotFoundException; +import org.hibernate.cfg.Environment; import org.hibernate.ejb.test.TestCase; /** @@ -30,11 +32,13 @@ em.getTransaction().begin(); music.setName( "Rock" ); try { + em.flush(); fail("Should raise an optimistic lock exception"); } catch( OptimisticLockException e) { //success + assertEquals( music, e.getEntity() ); } catch( Exception e ) { fail("Should raise an optimistic lock exception"); @@ -60,6 +64,13 @@ } } + @Override + public Map getConfig() { + Map config = super.getConfig(); + config.put( Environment.BATCH_VERSIONED_DATA, "false"); + return config; + } + public Class[] getAnnotatedClasses() { return new Class[] { Music.class Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/AssociationOverride.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/AssociationOverride.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/AssociationOverride.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -8,9 +8,7 @@ import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.FIELD; -/** - * @author Emmanuel Bernard - */ + @Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME) public @interface AssociationOverride { String name(); Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/CascadeType.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/CascadeType.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/CascadeType.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -3,6 +3,30 @@ package javax.persistence; /** + * Defines the set of cascadable operations that are propagated to the associated entity. + * The value cascade=ALL is equivalent to cascade={PERSIST, MERGE, REMOVE, REFRESH}. + * * @author Emmanuel Bernard */ -public enum CascadeType { ALL, PERSIST, MERGE, REMOVE, REFRESH }; +public enum CascadeType { + /** + * Cascade all operations + */ + ALL, + /** + * Cascade persist operations + */ + PERSIST, + /** + * Cascade merge operations + */ + MERGE, + /** + * Cascade remove operations + */ + REMOVE, + /** + * Cascade refresh operations + */ + REFRESH +} Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/DiscriminatorType.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/DiscriminatorType.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/DiscriminatorType.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -3,6 +3,21 @@ package javax.persistence; /** + * Defines supported types of the discriminator column + * * @author Emmanuel Bernard */ -public enum DiscriminatorType { STRING, CHAR, INTEGER }; +public enum DiscriminatorType { + /** + * String as the discriminator type + */ + STRING, + /** + * Single character as the discriminator type + */ + CHAR, + /** + * Integer as the discriminator type + */ + INTEGER +}; Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/EntityManager.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/EntityManager.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/EntityManager.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -3,10 +3,23 @@ /** * Interface used to interact with the persistence context. + * + * An EntityManager instance is associated with a persistence context. A persistence context is a set of + * entity instances in which for any persistent entity identity there is a unique entity instance. + * Within the persistence context, the entity instances and their lifecycle are managed. This interface + * defines the methods that are used to interact with the persistence context. The EntityManager API is + * used to create and remove persistent entity instances, to find entities by their primary key, and to + * query over entities. + * + * The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit. + * A persistence unit defines the set of all classes that are related or grouped by the application, and + * which must be colocated in their mapping to a single database. + * + * @author Emmanuel Bernard */ public interface EntityManager { /** - * Make an instance managed and persistent. + * Make an entity instance managed and persistent. * * @param entity * @throws EntityExistsException if the entity already exists. @@ -14,6 +27,7 @@ * operation is invoked, or the EntityExistsException or * another PersistenceException may be thrown at commit * time.) + * @throws IllegalStateException if this EntityManager has been closed. * @throws IllegalArgumentException if not an entity * @throws TransactionRequiredException if invoked on a * container-managed entity manager of type @@ -28,6 +42,7 @@ * * @param entity * @return the instance that the state was merged to + * @throws IllegalStateException if this EntityManager has been closed * @throws IllegalArgumentException if instance is not an * entity or is a removed entity * @throws TransactionRequiredException if invoked on a @@ -41,6 +56,7 @@ * Remove the entity instance. * * @param entity + * @throws IllegalStateException if this EntityManager has been closed * @throws IllegalArgumentException if not an entity * or if a detached entity * @throws TransactionRequiredException if invoked on a @@ -57,6 +73,7 @@ * @param primaryKey * @return the found entity instance or null * if the entity does not exist + * @throws IllegalStateException if this EntityManager has been closed * @throws IllegalArgumentException if the first argument does * not denote an entity type or the second * argument is not a valid type for that @@ -78,6 +95,7 @@ * @param entityClass * @param primaryKey * @return the found entity instance + * @throws IllegalStateException if this EntityManager has been closed * @throws IllegalArgumentException if the first argument does * not denote an entity type or the second * argument is not a valid type for that @@ -91,6 +109,7 @@ * Synchronize the persistence context to the * underlying database. * + * @throws IllegalStateException if this EntityManager has been closed * @throws TransactionRequiredException if there is * no transaction * @throws PersistenceException if the flush fails @@ -102,6 +121,7 @@ * in the persistence context. * * @param flushMode + * @throws IllegalStateException if this EntityManager has been closed */ public void setFlushMode(FlushModeType flushMode); @@ -110,6 +130,7 @@ * in the persistence context. * * @return flushMode + * @throws IllegalStateException if this EntityManager has been closed */ public FlushModeType getFlushMode(); @@ -119,6 +140,7 @@ * * @param entity * @param lockMode + * @throws IllegalStateException if this EntityManager has been closed * @throws PersistenceException if an unsupported lock call * is made * @throws IllegalArgumentException if the instance is not @@ -133,6 +155,7 @@ * overwriting changes made to the entity, if any. * * @param entity + * @throws IllegalStateException if this EntityManager has been closed * @throws IllegalArgumentException if not an entity * or entity is not managed * @throws TransactionRequiredException if invoked on a @@ -149,6 +172,8 @@ * entities to become detached. Changes made to entities that * have not been flushed to the database will not be * persisted. + * + * @throws IllegalStateException if this EntityManager has been closed */ public void clear(); @@ -157,7 +182,8 @@ * context. * * @param entity - * @return + * @return <code>true</code> if the instance belongs to the current persistence context. + * @throws IllegalStateException if this EntityManager has been closed * @throws IllegalArgumentException if not an entity */ public boolean contains(Object entity); @@ -168,6 +194,7 @@ * * @param ejbqlString an EJB QL query string * @return the new query instance + * @throws IllegalStateException if this EntityManager has been closed * @throws IllegalArgumentException if query string is not valid */ public Query createQuery(String ejbqlString); @@ -178,6 +205,7 @@ * * @param name the name of a query defined in metadata * @return the new query instance + * @throws IllegalStateException if this EntityManager has been closed * @throws IllegalArgumentException if a query has not been * defined with the given name */ @@ -189,6 +217,7 @@ * * @param sqlString a native SQL query string * @return the new query instance + * @throws IllegalStateException if this EntityManager has been closed */ public Query createNativeQuery(String sqlString); @@ -199,6 +228,7 @@ * @param sqlString a native SQL query string * @param resultClass the class of the resulting instance(s) * @return the new query instance + * @throws IllegalStateException if this EntityManager has been closed */ public Query createNativeQuery(String sqlString, Class resultClass); @@ -209,6 +239,7 @@ * @param sqlString a native SQL query string * @param resultSetMapping the name of the result set mapping * @return the new query instance + * @throws IllegalStateException if this EntityManager has been closed */ public Query createNativeQuery(String sqlString, String resultSetMapping); @@ -219,6 +250,7 @@ * of the active transaction to associate it with the current * JTA transaction. * + * @throws IllegalStateException if this EntityManager has been closed * @throws TransactionRequiredException if there is * no transaction. */ @@ -228,7 +260,7 @@ * Return the underlying provider object for the EntityManager, if available. * The result of this method is implementation specific * - * @return + * @throws IllegalStateException if this EntityManager has been closed */ public Object getDelegate(); @@ -242,8 +274,7 @@ * associated with an active transaction, the persistence * context remains managed until the transaction completes. * - * @throws IllegalStateException if the EntityManager - * is container-managed. + * @throws IllegalStateException if the EntityManager is container-managed or has been already closed */ public void close(); Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/EntityManagerFactory.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/EntityManagerFactory.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/EntityManagerFactory.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -3,6 +3,15 @@ import java.util.Map; +/** + * The EntityManagerFactory interface is used by the application to obtain an + * application-managed entity manager. When the application has finished using + * the entity manager factory, and/or at application shutdown, the application + * should close the entity manager factory. Once an EntityManagerFactory has been + * closed, all its entity managers are considered to be in the closed state. + * + * @author Emmanuel Bernard + */ public interface EntityManagerFactory { /** @@ -33,8 +42,8 @@ void close(); /** - * Indicates whether the factory is open. Returns true - * until the factory has been closed. + * Indicates whether or not this factory is open. Returns + * true until a call to close has been made. */ public boolean isOpen(); } \ No newline at end of file Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/EntityTransaction.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/EntityTransaction.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/EntityTransaction.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -1,6 +1,13 @@ //$Id$ package javax.persistence; +/** + * The EntityTransaction interface is used to control resource transactions + * on resource-local entity managers. The EntityManager.getTransaction() + * method returns the EntityTransaction interface. + * + * @author Emmanuel Bernard + */ public interface EntityTransaction { /** * Start a resource transaction. Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/EnumType.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/EnumType.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/EnumType.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -2,9 +2,18 @@ package javax.persistence; /** + * Defines mapping for the enumerated types. The constants of this enumerated type specify how persistent + * property or field should be persisted as a enumerated type. + * * @author Emmanuel Bernard */ public enum EnumType { + /** + * Persist enumerated type property or field as an integer + */ ORDINAL, + /** + * Persist enumerated type property or field as a string + */ STRING } Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/FetchType.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/FetchType.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/FetchType.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -3,6 +3,22 @@ package javax.persistence; /** + * Defines strategies for fetching data from the database. + * The EAGER strategy is a requirement on the persistence provider runtime that data must + * be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that + * data should be fetched lazily when it is first accessed. The implementation is permitted to + * eagerly fetch data for which the LAZY strategy hint has been specified. In particular, lazy + * fetching might only be available for Basic mappings for which property-based access is used. + * * @author Emmanuel Bernard */ -public enum FetchType { LAZY, EAGER }; +public enum FetchType { + /** + * Defines that data must be lazily fetched + */ + LAZY, + /** + * Defines that data must be eagerly fetched + */ + EAGER +}; Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/FlushModeType.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/FlushModeType.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/FlushModeType.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -2,9 +2,28 @@ package javax.persistence; /** + * Flush mode setting. + * + * When queries are executed within a transaction, if FlushModeType.AUTO is set on the Query object, + * or if the flush mode setting for the persistence context is AUTO (the default) and a flush mode + * setting has not been specified for the Query object, the persistence provider is responsible for + * ensuring that all updates to the state of all entities in the persistence context which could + * potentially affect the result of the query are visible to the processing of the query. + * The persistence provider implementation may achieve this by flushing those entities to the database + * or by some other means. If FlushModeType.COMMIT is set, the effect of updates made to entities in the + * persistence context upon queries is unspecified. + * + * If there is no transaction active, the persistence provider must not flush to the database. + * * @author Gavin King */ -public enum FlushModeType { +public enum FlushModeType { + /** + * Flushing must occur only at transaction commit + */ COMMIT, + /** + * (Default) Flushing to occur at query execution + */ AUTO } Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/OptimisticLockException.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/OptimisticLockException.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/OptimisticLockException.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -2,13 +2,23 @@ package javax.persistence; /** + * Thrown by the persistence provider when an optimistic locking conflict occurs. + * This exception may be thrown as part of an API call, a flush or at commit time. + * The current transaction, if one is active, will be marked for rollback. + * * @author Emmanuel Bernard */ public class OptimisticLockException extends PersistenceException { + private Object entity; + public OptimisticLockException() { super(); } + public OptimisticLockException(Object entity) { + this.entity = entity; + } + public OptimisticLockException(Throwable cause) { super( cause ); } @@ -20,4 +30,13 @@ public OptimisticLockException(String message, Throwable cause) { super( message, cause ); } + + public OptimisticLockException(String message, Throwable cause, Object entity) { + super( message, cause ); + this.entity = entity; + } + + public Object getEntity() { + return entity; + } } Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/Persistence.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/Persistence.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/Persistence.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -21,12 +21,26 @@ public static String PERSISTENCE_PROVIDER = PersistenceProvider.class.getName(); - protected static Set<PersistenceProvider> providers = new HashSet<PersistenceProvider>(); + protected static final Set<PersistenceProvider> providers = new HashSet<PersistenceProvider>(); + /** + * Create and return an EntityManagerFactory for the named persistence unit. + * + * @param persistenceUnitName The name of the persistence unit + * @return The factory that creates EntityManagers configured according to the specified persistence unit + */ public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName) { return createEntityManagerFactory( persistenceUnitName, null ); } + /** + * Create and return an EntityManagerFactory for the named persistence unit using the given properties. + * + * @param persistenceUnitName The name of the persistence unit + * @param properties Additional properties to use when creating the factory. The values of these properties override + * any values that may have been configured elsewhere + * @return The factory that creates EntityManagers configured according to the specified persistence unit + */ public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) { EntityManagerFactory emf = null; Added: trunk/HibernateExt/ejb-api/src/javax/persistence/PersistenceProperty.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/PersistenceProperty.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/PersistenceProperty.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -0,0 +1,22 @@ +//$Id: $ +package javax.persistence; + +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; + +/** + * Describes a single container or persistence provider property. + * Vendor specific properties may be included in the set of properties, and are passed to the persistence + * provider by the container when the entity manager is created. + * Properties that are not recognized by a vendor will be ignored. + * + * @author Emmanuel Bernard + */ +@Target({}) +@Retention(RUNTIME) +public @interface PersistenceProperty { + String name(); + + String value(); +} \ No newline at end of file Modified: trunk/HibernateExt/ejb-api/src/javax/persistence/Query.java =================================================================== --- trunk/HibernateExt/ejb-api/src/javax/persistence/Query.java 2006-08-08 17:12:21 UTC (rev 10232) +++ trunk/HibernateExt/ejb-api/src/javax/persistence/Query.java 2006-08-08 17:14:16 UTC (rev 10233) @@ -14,6 +14,7 @@ * Execute the query and return the query results as a List. * * @return a list of the results + * @throws IllegalStateException f called for a Java Persistence query language UPDATE or DELETE statement */ public List getResultList(); @@ -22,7 +23,8 @@ * * @return the result * @throws EntityNotFoundException if there is no result - * @throws NonUniqueResultException if more than one result + * @throws NonUniqueResultException if more than one resul + * @throws IllegalStateException f called for a Java Persistence query language UPDATE or DELETE statement */ public Object getSingleResult(); @@ -30,6 +32,8 @@ * Execute an update or delete statement. * * @return the number of entities updated or deleted + * @throws IllegalStateException if called for a Java Persistence query language SELECT statement + * @throws TransactionRequiredException if there is no transaction */ public int executeUpdate(); @@ -68,7 +72,7 @@ * @param name the parameter name * @param value * @return the same query instance - * @throws IllegalArgumentException if parameter name does not* correspond to parameter in query + * @throws IllegalArgumentException if parameter name does not correspond to parameter in query * string or argument is of incorrect type */ public Query setParameter(String name, Object value); |