/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2008, 2012, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.hibernate.loader.entity.plan;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.loader.Loader;
import org.hibernate.loader.entity.UniqueEntityLoader;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.type.Type;

import org.jboss.logging.Logger;

The base contract for UniqueEntityLoader implementations capable of performing batch-fetch loading of entities using multiple primary key values in the SQL WHERE clause.

Typically these are
Author:Gavin King, Steve Ebersole
See Also:
/** * The base contract for UniqueEntityLoader implementations capable of performing batch-fetch loading of entities * using multiple primary key values in the SQL <tt>WHERE</tt> clause. * <p/> * Typically these are * * @author Gavin King * @author Steve Ebersole * * @see org.hibernate.loader.entity.BatchingEntityLoaderBuilder * @see org.hibernate.loader.entity.UniqueEntityLoader */
public abstract class BatchingEntityLoader implements UniqueEntityLoader { private static final Logger log = Logger.getLogger( BatchingEntityLoader.class ); private final EntityPersister persister; public BatchingEntityLoader(EntityPersister persister) { this.persister = persister; } public EntityPersister persister() { return persister; } @Override @Deprecated public Object load(Serializable id, Object optionalObject, SessionImplementor session) { return load( id, optionalObject, session, LockOptions.NONE ); } protected QueryParameters buildQueryParameters( Serializable id, Serializable[] ids, Object optionalObject, LockOptions lockOptions) { Type[] types = new Type[ids.length]; Arrays.fill( types, persister().getIdentifierType() ); QueryParameters qp = new QueryParameters(); qp.setPositionalParameterTypes( types ); qp.setPositionalParameterValues( ids ); qp.setOptionalObject( optionalObject ); qp.setOptionalEntityName( persister().getEntityName() ); qp.setOptionalId( id ); qp.setLockOptions( lockOptions ); return qp; } protected Object getObjectFromList(List results, Serializable id, SessionImplementor session) { for ( Object obj : results ) { final boolean equal = persister.getIdentifierType().isEqual( id, session.getContextEntityIdentifier( obj ), session.getFactory() ); if ( equal ) { return obj; } } return null; } protected Object doBatchLoad( Serializable id, Loader loaderToUse, SessionImplementor session, Serializable[] ids, Object optionalObject, LockOptions lockOptions) { if ( log.isDebugEnabled() ) { log.debugf( "Batch loading entity: %s", MessageHelper.infoString( persister, ids, session.getFactory() ) ); } QueryParameters qp = buildQueryParameters( id, ids, optionalObject, lockOptions ); try { final List results = loaderToUse.doQueryAndInitializeNonLazyCollections( session, qp, false ); log.debug( "Done entity batch load" ); return getObjectFromList(results, id, session); } catch ( SQLException sqle ) { throw session.getFactory().getSQLExceptionHelper().convert( sqle, "could not load an entity batch: " + MessageHelper.infoString( persister(), ids, session.getFactory() ), loaderToUse.getSQLString() ); } } }