~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/loader/AbstractEntityJoinWalker.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: AbstractEntityJoinWalker.java 11081 2007-01-23 16:31:13Z steve.ebersole@jboss.com $
 
2
package org.hibernate.loader;
 
3
 
 
4
import java.util.ArrayList;
 
5
import java.util.List;
 
6
import java.util.Map;
 
7
 
 
8
import org.hibernate.FetchMode;
 
9
import org.hibernate.LockMode;
 
10
import org.hibernate.MappingException;
 
11
import org.hibernate.engine.CascadeStyle;
 
12
import org.hibernate.engine.SessionFactoryImplementor;
 
13
import org.hibernate.persister.entity.Loadable;
 
14
import org.hibernate.persister.entity.OuterJoinLoadable;
 
15
import org.hibernate.sql.JoinFragment;
 
16
import org.hibernate.sql.Select;
 
17
import org.hibernate.type.AssociationType;
 
18
import org.hibernate.util.CollectionHelper;
 
19
 
 
20
/**
 
21
 * Abstract walker for walkers which begin at an entity (criteria
 
22
 * queries and entity loaders).
 
23
 *
 
24
 * @author Gavin King
 
25
 */
 
26
public abstract class AbstractEntityJoinWalker extends JoinWalker {
 
27
 
 
28
        private final OuterJoinLoadable persister;
 
29
        private String alias;
 
30
 
 
31
        public AbstractEntityJoinWalker(OuterJoinLoadable persister, SessionFactoryImplementor factory, Map enabledFilters) {
 
32
                super( factory, enabledFilters );
 
33
                this.persister = persister;
 
34
                alias = generateRootAlias( persister.getEntityName() );
 
35
        }
 
36
 
 
37
        protected final void initAll(
 
38
                final String whereString,
 
39
                final String orderByString,
 
40
                final LockMode lockMode)
 
41
        throws MappingException {
 
42
 
 
43
                walkEntityTree( persister, getAlias() );
 
44
 
 
45
                List allAssociations = new ArrayList();
 
46
                allAssociations.addAll(associations);
 
47
                allAssociations.add( new OuterJoinableAssociation(
 
48
                                persister.getEntityType(),
 
49
                                null,
 
50
                                null,
 
51
                                alias,
 
52
                                JoinFragment.LEFT_OUTER_JOIN,
 
53
                                getFactory(),
 
54
                                CollectionHelper.EMPTY_MAP
 
55
                        ) );
 
56
 
 
57
                initPersisters(allAssociations, lockMode);
 
58
                initStatementString( whereString, orderByString, lockMode);
 
59
        }
 
60
 
 
61
        protected final void initProjection(
 
62
                final String projectionString,
 
63
                final String whereString,
 
64
                final String orderByString,
 
65
                final String groupByString,
 
66
                final LockMode lockMode)
 
67
        throws MappingException {
 
68
                walkEntityTree( persister, getAlias() );
 
69
                persisters = new Loadable[0];
 
70
                initStatementString(projectionString, whereString, orderByString, groupByString, lockMode);
 
71
        }
 
72
 
 
73
        private void initStatementString(
 
74
                final String condition,
 
75
                final String orderBy,
 
76
                final LockMode lockMode)
 
77
        throws MappingException {
 
78
                initStatementString(null, condition, orderBy, "", lockMode);
 
79
        }
 
80
 
 
81
        private void initStatementString(
 
82
                        final String projection,
 
83
                        final String condition,
 
84
                        final String orderBy,
 
85
                        final String groupBy,
 
86
                        final LockMode lockMode) 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
                                                getDialect().appendLockHint( lockMode, 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
        protected String getWhereFragment() throws MappingException {
 
119
                // here we do not bother with the discriminator.
 
120
                return persister.whereJoinFragment(alias, true, true);
 
121
        }
 
122
 
 
123
        /**
 
124
         * The superclass deliberately excludes collections
 
125
         */
 
126
        protected boolean isJoinedFetchEnabled(AssociationType type, FetchMode config, CascadeStyle cascadeStyle) {
 
127
                return isJoinedFetchEnabledInMapping(config, type);
 
128
        }
 
129
 
 
130
        public abstract String getComment();
 
131
 
 
132
        protected final Loadable getPersister() {
 
133
                return persister;
 
134
        }
 
135
 
 
136
        protected final String getAlias() {
 
137
                return alias;
 
138
        }
 
139
 
 
140
        public String toString() {
 
141
                return getClass().getName() + '(' + getPersister().getEntityName() + ')';
 
142
        }
 
143
}