Issue Type: Bug Bug
Affects Versions: 4.1.6
Assignee: Brett Meyer
Attachments: log.txt
Components: core, entity-manager
Created: 13/Dec/12 7:28 AM
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()
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()
if ( !options.isAllowProxyCreation() ) {
			impl = load( event, persister, keyToLoad, options );
			if ( impl == null && !options.isAllowNulls() ) {
				event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( persister.getEntityName(), keyToLoad.getIdentifier());
			}
		}
Project: Hibernate ORM
Priority: Major Major
Reporter: Zbyněk Roubalík
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira