HHH-7841 - Redesign Loader

This commit is contained in:
Steve Ebersole 2013-05-24 12:01:54 -05:00
parent 5ea40ce3f5
commit a5862017d8
2 changed files with 27 additions and 22 deletions

View File

@ -57,17 +57,13 @@ public abstract class AbstractEntityLoader extends OuterJoinLoader
} }
/** @Override
* {@inheritDoc}
*/
public Object load(Serializable id, Object optionalObject, SessionImplementor session) { public Object load(Serializable id, Object optionalObject, SessionImplementor session) {
// this form is deprecated! // this form is deprecated!
return load( id, optionalObject, session, LockOptions.NONE ); return load( id, optionalObject, session, LockOptions.NONE );
} }
/** @Override
* {@inheritDoc}
*/
public Object load(Serializable id, Object optionalObject, SessionImplementor session, LockOptions lockOptions) { public Object load(Serializable id, Object optionalObject, SessionImplementor session, LockOptions lockOptions) {
return load( session, id, optionalObject, id, lockOptions ); return load( session, id, optionalObject, id, lockOptions );
} }

View File

@ -25,56 +25,65 @@ package org.hibernate.persister.entity;
import java.io.Serializable; import java.io.Serializable;
import org.jboss.logging.Logger;
import org.hibernate.FlushMode; import org.hibernate.FlushMode;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.AbstractQueryImpl; import org.hibernate.internal.AbstractQueryImpl;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.loader.entity.UniqueEntityLoader; import org.hibernate.loader.entity.UniqueEntityLoader;
/** /**
* Not really a <tt>Loader</tt>, just a wrapper around a * Not really a Loader, just a wrapper around a named query. Used when the metadata has named a query to use for
* named query. * loading an entity (using {@link org.hibernate.annotations.Loader} or {@code <loader/>}).
* *
* @author Gavin King * @author Gavin King
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public final class NamedQueryLoader implements UniqueEntityLoader { public final class NamedQueryLoader implements UniqueEntityLoader {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( NamedQueryLoader.class );
private final String queryName; private final String queryName;
private final EntityPersister persister; private final EntityPersister persister;
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, NamedQueryLoader.class.getName()); /**
* Constructs the NamedQueryLoader
*
* @param queryName The name of the named query to use
* @param persister The corresponding persister for the entity we are loading
*/
public NamedQueryLoader(String queryName, EntityPersister persister) { public NamedQueryLoader(String queryName, EntityPersister persister) {
super(); super();
this.queryName = queryName; this.queryName = queryName;
this.persister = persister; this.persister = persister;
} }
@Override
public Object load(Serializable id, Object optionalObject, SessionImplementor session, LockOptions lockOptions) { public Object load(Serializable id, Object optionalObject, SessionImplementor session, LockOptions lockOptions) {
if (lockOptions != null) LOG.debug("Ignoring lock-options passed to named query loader"); if ( lockOptions != null ) {
LOG.debug( "Ignoring lock-options passed to named query loader" );
}
return load( id, optionalObject, session ); return load( id, optionalObject, session );
} }
@Override
public Object load(Serializable id, Object optionalObject, SessionImplementor session) { public Object load(Serializable id, Object optionalObject, SessionImplementor session) {
LOG.debugf("Loading entity: %s using named query: %s", persister.getEntityName(), queryName); LOG.debugf( "Loading entity: %s using named query: %s", persister.getEntityName(), queryName );
AbstractQueryImpl query = (AbstractQueryImpl) session.getNamedQuery(queryName); // IMPL NOTE: essentially we perform the named query (which loads the entity into the PC), and then
// do an internal lookup of the entity from the PC.
final AbstractQueryImpl query = (AbstractQueryImpl) session.getNamedQuery( queryName );
if ( query.hasNamedParameters() ) { if ( query.hasNamedParameters() ) {
query.setParameter( query.setParameter( query.getNamedParameters()[0], id, persister.getIdentifierType() );
query.getNamedParameters()[0],
id,
persister.getIdentifierType()
);
} }
else { else {
query.setParameter( 0, id, persister.getIdentifierType() ); query.setParameter( 0, id, persister.getIdentifierType() );
} }
query.setOptionalId(id);
query.setOptionalId( id );
query.setOptionalEntityName( persister.getEntityName() ); query.setOptionalEntityName( persister.getEntityName() );
query.setOptionalObject(optionalObject); query.setOptionalObject( optionalObject );
query.setFlushMode( FlushMode.MANUAL ); query.setFlushMode( FlushMode.MANUAL );
query.list(); query.list();