HHH-10302 Extract helper method for LoadEvent reuse and adapt code formatting to Hibernate style

This commit is contained in:
Sanne Grinovero 2015-11-26 16:11:47 +00:00
parent 85db76431c
commit f0f544472c
2 changed files with 44 additions and 44 deletions

View File

@ -105,7 +105,7 @@ public class LoadEvent extends AbstractEvent {
this.instanceToLoad = instanceToLoad; this.instanceToLoad = instanceToLoad;
this.lockOptions = lockOptions; this.lockOptions = lockOptions;
this.isAssociationFetch = isAssociationFetch; this.isAssociationFetch = isAssociationFetch;
this.postLoadEvent = new PostLoadEvent(source); this.postLoadEvent = new PostLoadEvent( source );
} }
public Serializable getEntityId() { public Serializable getEntityId() {

View File

@ -941,22 +941,22 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
public void load(Object object, Serializable id) throws HibernateException { public void load(Object object, Serializable id) throws HibernateException {
LoadEvent event = loadEvent; LoadEvent event = loadEvent;
loadEvent = null; loadEvent = null;
if(event == null) { if ( event == null ) {
event = new LoadEvent( id, object, this ); event = new LoadEvent( id, object, this );
} else { } else {
event.setEntityClassName(null); event.setEntityClassName( null );
event.setEntityId(id); event.setEntityId( id );
event.setInstanceToLoad(object); event.setInstanceToLoad( object );
event.setLockMode(LoadEvent.DEFAULT_LOCK_MODE); event.setLockMode( LoadEvent.DEFAULT_LOCK_MODE );
event.setLockScope(LoadEvent.DEFAULT_LOCK_OPTIONS.getScope()); event.setLockScope( LoadEvent.DEFAULT_LOCK_OPTIONS.getScope() );
event.setLockTimeout(LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut()); event.setLockTimeout( LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut() );
} }
fireLoad( event, LoadEventListener.RELOAD ); fireLoad( event, LoadEventListener.RELOAD );
if(loadEvent == null) { if ( loadEvent == null ) {
event.setEntityClassName(null); event.setEntityClassName( null );
event.setEntityId(null); event.setEntityId( null );
event.setInstanceToLoad(null); event.setInstanceToLoad( null );
event.setResult(null); event.setResult( null );
loadEvent = event; loadEvent = event;
} }
} }
@ -994,23 +994,14 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
} }
LoadEvent event = loadEvent; LoadEvent event = loadEvent;
loadEvent = null; loadEvent = null;
if(event == null) { event = recycleEventInstance( event, id, entityName );
event = new LoadEvent( id, entityName, true, this );
} else {
event.setEntityClassName(entityName);
event.setEntityId(id);
event.setInstanceToLoad(null);
event.setLockMode(LoadEvent.DEFAULT_LOCK_MODE);
event.setLockScope(LoadEvent.DEFAULT_LOCK_OPTIONS.getScope());
event.setLockTimeout(LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut());
}
fireLoad( event, LoadEventListener.IMMEDIATE_LOAD ); fireLoad( event, LoadEventListener.IMMEDIATE_LOAD );
Object result = event.getResult(); Object result = event.getResult();
if(loadEvent == null) { if ( loadEvent == null ) {
event.setEntityClassName(null); event.setEntityClassName( null );
event.setEntityId(null); event.setEntityId( null );
event.setInstanceToLoad(null); event.setInstanceToLoad( null );
event.setResult(null); event.setResult( null );
loadEvent = event; loadEvent = event;
} }
return result; return result;
@ -1028,31 +1019,40 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
LoadEvent event = loadEvent; LoadEvent event = loadEvent;
loadEvent = null; loadEvent = null;
if(event == null) { event = recycleEventInstance( event, id, entityName );
event = new LoadEvent( id, entityName, true, this );
} else {
event.setEntityClassName(entityName);
event.setEntityId(id);
event.setInstanceToLoad(null);
event.setLockMode(LoadEvent.DEFAULT_LOCK_MODE);
event.setLockScope(LoadEvent.DEFAULT_LOCK_OPTIONS.getScope());
event.setLockTimeout(LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut());
}
fireLoad( event, type ); fireLoad( event, type );
Object result = event.getResult(); Object result = event.getResult();
if ( !nullable ) { if ( !nullable ) {
UnresolvableObjectException.throwIfNull(result, id, entityName ); UnresolvableObjectException.throwIfNull( result, id, entityName );
} }
if(loadEvent == null) { if ( loadEvent == null ) {
event.setEntityClassName(null); event.setEntityClassName( null );
event.setEntityId(null); event.setEntityId( null );
event.setInstanceToLoad(null); event.setInstanceToLoad( null );
event.setResult(null); event.setResult( null );
loadEvent = event; loadEvent = event;
} }
return result; return result;
} }
/**
* Helper to avoid creating many new instances of LoadEvent: it's an allocation hot spot.
*/
private LoadEvent recycleEventInstance(final LoadEvent event, final Serializable id, final String entityName) {
if ( event == null ) {
return new LoadEvent( id, entityName, true, this );
}
else {
event.setEntityClassName( entityName );
event.setEntityId( id );
event.setInstanceToLoad( null );
event.setLockMode( LoadEvent.DEFAULT_LOCK_MODE );
event.setLockScope( LoadEvent.DEFAULT_LOCK_OPTIONS.getScope() );
event.setLockTimeout( LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut() );
return event;
}
}
@Override @Override
public <T> T load(Class<T> entityClass, Serializable id, LockMode lockMode) throws HibernateException { public <T> T load(Class<T> entityClass, Serializable id, LockMode lockMode) throws HibernateException {
return this.byId( entityClass ).with( new LockOptions( lockMode ) ).getReference( id ); return this.byId( entityClass ).with( new LockOptions( lockMode ) ).getReference( id );