KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > loader > BasicLoader


1 //$Id: BasicLoader.java,v 1.9 2005/05/19 07:51:31 steveebersole Exp $
2
package org.hibernate.loader;
3
4 import org.hibernate.engine.SessionFactoryImplementor;
5 import org.hibernate.persister.entity.Loadable;
6 import org.hibernate.persister.collection.CollectionPersister;
7
8 /**
9  * Uses the default mapping from property to result set column
10  * alias defined by the entities' persisters. Used when Hibernate
11  * is generating result set column aliases.
12  *
13  * @author Gavin King
14  */

15 public abstract class BasicLoader extends Loader {
16
17     protected static final String JavaDoc[] NO_SUFFIX = {""};
18
19     private EntityAliases[] descriptors;
20     private CollectionAliases[] collectionDescriptors;
21
22     public BasicLoader(SessionFactoryImplementor factory) {
23         super(factory);
24     }
25     
26     protected final EntityAliases[] getEntityAliases() {
27         return descriptors;
28     }
29
30     protected final CollectionAliases[] getCollectionAliases() {
31         return collectionDescriptors;
32     }
33
34     protected abstract String JavaDoc[] getSuffixes();
35     protected abstract String JavaDoc[] getCollectionSuffixes();
36
37     protected void postInstantiate() {
38         Loadable[] persisters = getEntityPersisters();
39         String JavaDoc[] suffixes = getSuffixes();
40         descriptors = new EntityAliases[persisters.length];
41         for ( int i=0; i<descriptors.length; i++ ) {
42             descriptors[i] = new DefaultEntityAliases( persisters[i], suffixes[i] );
43         }
44
45         CollectionPersister[] collectionPersisters = getCollectionPersisters();
46         if ( collectionPersisters != null ) {
47             String JavaDoc[] collectionSuffixes = getCollectionSuffixes();
48             collectionDescriptors = new CollectionAliases[collectionPersisters.length];
49             for ( int i = 0; i < collectionPersisters.length; i++ ) {
50                 collectionDescriptors[i] = new GeneratedCollectionAliases( collectionPersisters[i], collectionSuffixes[i] );
51             }
52         }
53         else {
54             collectionDescriptors = null;
55         }
56     }
57     
58     /**
59      * Utility method that generates 0_, 1_ suffixes. Subclasses don't
60      * necessarily need to use this algorithm, but it is intended that
61      * they will in most cases.
62      */

63     public static String JavaDoc[] generateSuffixes(int length) {
64         return generateSuffixes( 0, length );
65     }
66
67     public static String JavaDoc[] generateSuffixes(int seed, int length) {
68         if ( length == 0 ) return NO_SUFFIX;
69
70         String JavaDoc[] suffixes = new String JavaDoc[length];
71         for ( int i = 0; i < length; i++ ) {
72             suffixes[i] = Integer.toString( i + seed ) + "_";
73         }
74         return suffixes;
75     }
76
77 }
78
Popular Tags