HHH-7861 EntityManager.find() should return null instead of EntityNotFoundException (change was merged into org.hibernate.jpa.spi.AbstractEntityManagerImpl)

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

View File

@ -33,10 +33,13 @@ import org.jboss.logging.MessageLogger;
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.INFO;
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
* 15001 to 20000 inclusively.
@ -100,4 +103,11 @@ public interface EntityManagerMessageLogger extends CoreMessageLogger {
@LogMessage( level = INFO )
@Message( value = "Using provided datasource", id = 15012 )
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 );
}

View File

@ -887,6 +887,14 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
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 ) {
//the spec is silent about people doing remove() find() on the same PC
return null;