Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » loader » entity » [javadoc | source]
    1   /*
    2    * Hibernate, Relational Persistence for Idiomatic Java
    3    *
    4    * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
    5    * indicated by the @author tags or express copyright attribution
    6    * statements applied by the authors.  All third-party contributions are
    7    * distributed under license by Red Hat Middleware LLC.
    8    *
    9    * This copyrighted material is made available to anyone wishing to use, modify,
   10    * copy, or redistribute it subject to the terms and conditions of the GNU
   11    * Lesser General Public License, as published by the Free Software Foundation.
   12    *
   13    * This program is distributed in the hope that it will be useful,
   14    * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15    * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   16    * for more details.
   17    *
   18    * You should have received a copy of the GNU Lesser General Public License
   19    * along with this distribution; if not, write to:
   20    * Free Software Foundation, Inc.
   21    * 51 Franklin Street, Fifth Floor
   22    * Boston, MA  02110-1301  USA
   23    *
   24    */
   25   package org.hibernate.loader.entity;
   26   
   27   import java.sql.ResultSet;
   28   import java.sql.SQLException;
   29   import java.util.List;
   30   import java.util.Map;
   31   
   32   import org.slf4j.Logger;
   33   import org.slf4j.LoggerFactory;
   34   import org.hibernate.HibernateException;
   35   import org.hibernate.LockMode;
   36   import org.hibernate.MappingException;
   37   import org.hibernate.engine.SessionFactoryImplementor;
   38   import org.hibernate.engine.SessionImplementor;
   39   import org.hibernate.loader.JoinWalker;
   40   import org.hibernate.loader.OuterJoinLoader;
   41   import org.hibernate.persister.collection.QueryableCollection;
   42   import org.hibernate.persister.entity.OuterJoinLoadable;
   43   import org.hibernate.transform.ResultTransformer;
   44   import org.hibernate.type.Type;
   45   import org.hibernate.util.ArrayHelper;
   46   
   47   /**
   48    * 
   49    *
   50    * @author Gavin King
   51    */
   52   public class CollectionElementLoader extends OuterJoinLoader {
   53   	
   54   	private static final Logger log = LoggerFactory.getLogger(CollectionElementLoader.class);
   55   
   56   	private final OuterJoinLoadable persister;
   57   	private final Type keyType;
   58   	private final Type indexType;
   59   	private final String entityName;
   60   
   61   	public CollectionElementLoader(
   62   			QueryableCollection collectionPersister,
   63   			SessionFactoryImplementor factory, 
   64   			Map enabledFilters) 
   65   	throws MappingException {
   66   		super(factory, enabledFilters);
   67   
   68   		this.keyType = collectionPersister.getKeyType();
   69   		this.indexType = collectionPersister.getIndexType();
   70   		this.persister = (OuterJoinLoadable) collectionPersister.getElementPersister();
   71   		this.entityName = persister.getEntityName();
   72   		
   73   		JoinWalker walker = new EntityJoinWalker(
   74   				persister, 
   75   				ArrayHelper.join( 
   76   						collectionPersister.getKeyColumnNames(), 
   77   						collectionPersister.getIndexColumnNames()
   78   					),
   79   				1, 
   80   				LockMode.NONE, 
   81   				factory, 
   82   				enabledFilters
   83   			);
   84   		initFromWalker( walker );
   85   
   86   		postInstantiate();
   87   		
   88   		log.debug( "Static select for entity " + entityName + ": " + getSQLString() );
   89   
   90   	}
   91   
   92   	public Object loadElement(SessionImplementor session, Object key, Object index) 
   93   	throws HibernateException {
   94   		
   95   		List list = loadEntity(
   96   				session, 
   97   				key,
   98   				index,
   99   				keyType, 
  100   				indexType,
  101   				persister
  102   			);
  103   		
  104   		if ( list.size()==1 ) {
  105   			return list.get(0);
  106   		}
  107   		else if ( list.size()==0 ) {
  108   			return null;
  109   		}
  110   		else {
  111   			if ( getCollectionOwners()!=null ) {
  112   				return list.get(0);
  113   			}
  114   			else {
  115   				throw new HibernateException("More than one row was found");
  116   			}
  117   		}
  118   		
  119   	}
  120   
  121   	protected Object getResultColumnOrRow(
  122   		Object[] row,
  123   		ResultTransformer transformer,
  124   		ResultSet rs, SessionImplementor session)
  125   	throws SQLException, HibernateException {
  126   		return row[row.length-1];
  127   	}
  128   
  129   	protected boolean isSingleRowLoader() {
  130   		return true;
  131   	}
  132   
  133   	
  134   }

Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » loader » entity » [javadoc | source]