HHH-7861 EntityManager.find() should return null instead of EntityNotFoundException

This commit is contained in:
Scott Marlow 2012-12-13 11:53:43 -05:00
parent d339e94491
commit 2f0bd59431
2 changed files with 18 additions and 0 deletions

View File

@ -807,6 +807,14 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
return ( A ) getSession().get( entityClass, ( Serializable ) primaryKey ); return ( A ) getSession().get( entityClass, ( Serializable ) primaryKey );
} }
} }
catch ( EntityNotFoundException ignored ) {
// DefaultLoadEventListener.returnNarrowedProxy may throw ENFE (see HHH-7861 for details),
// which find() should not throw. Find() should return null if the entity was not found.
if ( LOG.isDebugEnabled() ) {
LOG.ignoringEntityNotFound( entityClass != null ? entityClass.getName(): null, ignored );
}
return null;
}
catch ( ObjectDeletedException e ) { catch ( ObjectDeletedException e ) {
//the spec is silent about people doing remove() find() on the same PC //the spec is silent about people doing remove() find() on the same PC
return null; return null;

View File

@ -33,10 +33,13 @@ import org.jboss.logging.MessageLogger;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import static org.jboss.logging.Logger.Level.DEBUG;
import static org.jboss.logging.Logger.Level.ERROR; import static org.jboss.logging.Logger.Level.ERROR;
import static org.jboss.logging.Logger.Level.INFO; import static org.jboss.logging.Logger.Level.INFO;
import static org.jboss.logging.Logger.Level.WARN; import static org.jboss.logging.Logger.Level.WARN;
import javax.persistence.EntityNotFoundException;
/** /**
* The jboss-logging {@link MessageLogger} for the hibernate-entitymanager module. It reserves message ids ranging from * The jboss-logging {@link MessageLogger} for the hibernate-entitymanager module. It reserves message ids ranging from
* 15001 to 20000 inclusively. * 15001 to 20000 inclusively.
@ -100,4 +103,11 @@ public interface EntityManagerMessageLogger extends CoreMessageLogger {
@LogMessage( level = INFO ) @LogMessage( level = INFO )
@Message( value = "Using provided datasource", id = 15012 ) @Message( value = "Using provided datasource", id = 15012 )
void usingProvidedDataSource(); void usingProvidedDataSource();
@LogMessage( level = DEBUG )
@Message( value = "Returning null (as required by JPA spec) rather than throwing EntityNotFoundException, " +
"as the entity (%s) does not exist", id = 15013 )
void ignoringEntityNotFound( String entityName,
@Cause EntityNotFoundException e );
} }