Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » loader » [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;
   26   
   27   import org.hibernate.engine.SessionFactoryImplementor;
   28   import org.hibernate.persister.entity.Loadable;
   29   import org.hibernate.persister.collection.CollectionPersister;
   30   import org.hibernate.type.BagType;
   31   import org.hibernate.HibernateException;
   32   
   33   /**
   34    * Uses the default mapping from property to result set column 
   35    * alias defined by the entities' persisters. Used when Hibernate
   36    * is generating result set column aliases.
   37    * 
   38    * @author Gavin King
   39    */
   40   public abstract class BasicLoader extends Loader {
   41   
   42   	protected static final String[] NO_SUFFIX = {""};
   43   
   44   	private EntityAliases[] descriptors;
   45   	private CollectionAliases[] collectionDescriptors;
   46   
   47   	public BasicLoader(SessionFactoryImplementor factory) {
   48   		super(factory);
   49   	}
   50   
   51   	protected final EntityAliases[] getEntityAliases() {
   52   		return descriptors;
   53   	}
   54   
   55   	protected final CollectionAliases[] getCollectionAliases() {
   56   		return collectionDescriptors;
   57   	}
   58   
   59   	protected abstract String[] getSuffixes();
   60   	protected abstract String[] getCollectionSuffixes();
   61   
   62   	protected void postInstantiate() {
   63   		Loadable[] persisters = getEntityPersisters();
   64   		String[] suffixes = getSuffixes();
   65   		descriptors = new EntityAliases[persisters.length];
   66   		for ( int i=0; i<descriptors.length; i++ ) {
   67   			descriptors[i] = new DefaultEntityAliases( persisters[i], suffixes[i] );
   68   		}
   69   
   70   		CollectionPersister[] collectionPersisters = getCollectionPersisters();
   71   		int bagCount = 0;
   72   		if ( collectionPersisters != null ) {
   73   			String[] collectionSuffixes = getCollectionSuffixes();
   74   			collectionDescriptors = new CollectionAliases[collectionPersisters.length];
   75   			for ( int i = 0; i < collectionPersisters.length; i++ ) {
   76   				if ( isBag( collectionPersisters[i] ) ) {
   77   					bagCount++;
   78   				}
   79   				collectionDescriptors[i] = new GeneratedCollectionAliases(
   80   						collectionPersisters[i],
   81   						collectionSuffixes[i]
   82   					);
   83   			}
   84   		}
   85   		else {
   86   			collectionDescriptors = null;
   87   		}
   88   		if ( bagCount > 1 ) {
   89   			throw new HibernateException( "cannot simultaneously fetch multiple bags" );
   90   		}
   91   	}
   92   
   93   	private boolean isBag(CollectionPersister collectionPersister) {
   94   		return collectionPersister.getCollectionType().getClass().isAssignableFrom( BagType.class );
   95   	}
   96   
   97   	/**
   98   	 * Utility method that generates 0_, 1_ suffixes. Subclasses don't
   99   	 * necessarily need to use this algorithm, but it is intended that
  100   	 * they will in most cases.
  101   	 */
  102   	public static String[] generateSuffixes(int length) {
  103   		return generateSuffixes( 0, length );
  104   	}
  105   
  106   	public static String[] generateSuffixes(int seed, int length) {
  107   		if ( length == 0 ) return NO_SUFFIX;
  108   
  109   		String[] suffixes = new String[length];
  110   		for ( int i = 0; i < length; i++ ) {
  111   			suffixes[i] = Integer.toString( i + seed ) + "_";
  112   		}
  113   		return suffixes;
  114   	}
  115   
  116   }

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