From 6423754bccc3e1d862fa3417abe2b06fd35b64c5 Mon Sep 17 00:00:00 2001 From: Scott Marlow Date: Thu, 13 Dec 2012 11:53:43 -0500 Subject: [PATCH] HHH-7861 EntityManager.find() should return null instead of EntityNotFoundException (change was merged into org.hibernate.jpa.spi.AbstractEntityManagerImpl) --- .../jpa/internal/EntityManagerMessageLogger.java | 10 ++++++++++ .../hibernate/jpa/spi/AbstractEntityManagerImpl.java | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerMessageLogger.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerMessageLogger.java index 95748174c4..49f35af5ae 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerMessageLogger.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerMessageLogger.java @@ -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 ); } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/spi/AbstractEntityManagerImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/spi/AbstractEntityManagerImpl.java index 19741179c2..d45bca15dc 100755 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/spi/AbstractEntityManagerImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/spi/AbstractEntityManagerImpl.java @@ -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;