We're updating the issue view to help you get more done. 

EntityManager.find() throws EntityNotFoundException when entity is not found

Description

EntityManager.find() throws EntityNotFoundException instead of returning null when entity is not found.
It happens in following scenario:
1. perform em.find(Entity.class, id)
2. DefaultLoadEventListener.returnNarrowedProxy() -> Entity proxy is found in session cache
3. DefaultLoadEventListener.doLoad() -> Entity is not resolved in any cache
4. It seems the entity is modified in DB in the meantime -> DefaultLoadEventListener.loadFromDatasource() returns null
5. therefore DefaultLoadEventListener.doLoad() returns null
6. DefaultLoadEventListener.load() returns null
7. DefaultLoadEventListener.returnNarrowedProxy() throws the exception

This is the relevant piece of code :

DefaultLoadEventListener.returnNarrowedProxy()

1 2 3 4 5 6 if ( !options.isAllowProxyCreation() ) { impl = load( event, persister, keyToLoad, options ); if ( impl == null ) { event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( persister.getEntityName(), keyToLoad.getIdentifier()); } }

there should be check before throwing the exception, something like this:

DefaultLoadEventListener.returnNarrowedProxy()

1 2 3 4 5 6 if ( !options.isAllowProxyCreation() ) { impl = load( event, persister, keyToLoad, options ); if ( impl == null && !options.isAllowNulls() ) { event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( persister.getEntityName(), keyToLoad.getIdentifier()); } }

Environment

None

Status

Assignee

Scott Marlow

Reporter

Zbyněk Roubalík

Affects versions

4.1.6

Priority

Major