KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: AbstractEntityJoinWalker.java,v 1.2 2005/06/13 20:27:15 oneovthafew Exp $
2
package org.hibernate.loader;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.hibernate.FetchMode;
9 import org.hibernate.LockMode;
10 import org.hibernate.MappingException;
11 import org.hibernate.engine.SessionFactoryImplementor;
12 import org.hibernate.persister.entity.Loadable;
13 import org.hibernate.persister.entity.OuterJoinLoadable;
14 import org.hibernate.sql.JoinFragment;
15 import org.hibernate.sql.Select;
16 import org.hibernate.type.AssociationType;
17 import org.hibernate.util.CollectionHelper;
18
19 /**
20  * Abstract walker for walkers which begin at an entity (criteria
21  * queries and entity loaders).
22  *
23  * @author Gavin King
24  */

25 public abstract class AbstractEntityJoinWalker extends JoinWalker {
26
27     private final OuterJoinLoadable persister;
28     private String JavaDoc alias;
29     
30     public AbstractEntityJoinWalker(OuterJoinLoadable persister, SessionFactoryImplementor factory, Map JavaDoc enabledFilters) {
31         super( factory, enabledFilters );
32         this.persister = persister;
33         alias = generateRootAlias( persister.getEntityName() );
34     }
35
36     protected final void initAll(
37         final String JavaDoc whereString,
38         final String JavaDoc orderByString,
39         final LockMode lockMode)
40     throws MappingException {
41         
42         walkEntityTree( persister, getAlias() );
43         
44         List JavaDoc allAssociations = new ArrayList JavaDoc();
45         allAssociations.addAll(associations);
46         allAssociations.add( new OuterJoinableAssociation(
47                 persister.getEntityType(),
48                 null,
49                 null,
50                 alias,
51                 JoinFragment.LEFT_OUTER_JOIN,
52                 getFactory(),
53                 CollectionHelper.EMPTY_MAP
54             ) );
55         
56         initPersisters(allAssociations, lockMode);
57         initStatementString( whereString, orderByString, lockMode);
58     }
59     
60     protected final void initProjection(
61         final String JavaDoc projectionString,
62         final String JavaDoc whereString,
63         final String JavaDoc orderByString,
64         final String JavaDoc groupByString,
65         final LockMode lockMode)
66     throws MappingException {
67         walkEntityTree( persister, getAlias() );
68         persisters = new Loadable[0];
69         initStatementString(projectionString, whereString, orderByString, groupByString, lockMode);
70     }
71
72     private void initStatementString(
73         final String JavaDoc condition,
74         final String JavaDoc orderBy,
75         final LockMode lockMode)
76     throws MappingException {
77         initStatementString(null, condition, orderBy, "", lockMode);
78     }
79     
80     private void initStatementString(
81             final String JavaDoc projection,
82             final String JavaDoc condition,
83             final String JavaDoc orderBy,
84             final String JavaDoc groupBy,
85             final LockMode lockMode)
86         throws MappingException {
87
88         final int joins = countEntityPersisters( associations );
89         suffixes = BasicLoader.generateSuffixes( joins+1 );
90
91         JoinFragment ojf = mergeOuterJoins( associations );
92         
93         Select select = new Select( getDialect() )
94             .setLockMode(lockMode)
95             .setSelectClause(
96                     projection==null ?
97                             persister.selectFragment( alias, suffixes[joins] ) + selectString(associations) :
98                             projection
99             )
100             .setFromClause(
101                 persister.fromTableFragment(alias) +
102                 persister.fromJoinFragment(alias, true, true)
103             )
104             .setWhereClause(condition)
105             .setOuterJoins(
106                 ojf.toFromFragmentString(),
107                 ojf.toWhereFragmentString() + getWhereFragment()
108             )
109             .setOrderByClause( orderBy( associations, orderBy ) )
110             .setGroupByClause(groupBy);
111
112         if ( getFactory().getSettings().isCommentsEnabled() ) {
113             select.setComment( getComment() );
114         }
115         sql = select.toStatementString();
116     }
117
118     /**
119      * Don't bother with the discriminator, unless overridded by subclass
120      */

121     protected String JavaDoc getWhereFragment() throws MappingException {
122         return persister.whereJoinFragment(alias, true, true);
123     }
124
125     /**
126      * The superclass deliberately excludes collections
127      */

128     protected boolean isJoinedFetchEnabled(AssociationType type, FetchMode config) {
129         return isJoinedFetchEnabledInMapping(config, type);
130     }
131
132     public abstract String JavaDoc getComment();
133
134     protected final Loadable getPersister() {
135         return persister;
136     }
137
138     protected final String JavaDoc getAlias() {
139         return alias;
140     }
141
142     public String JavaDoc toString() {
143         return getClass().getName() + '(' + getPersister().getEntityName() + ')';
144     }
145 }
146
Popular Tags