1 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.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 25 public abstract class AbstractEntityJoinWalker extends JoinWalker { 26 27 private final OuterJoinLoadable persister; 28 private String alias; 29 30 public AbstractEntityJoinWalker(OuterJoinLoadable persister, SessionFactoryImplementor factory, Map enabledFilters) { 31 super( factory, enabledFilters ); 32 this.persister = persister; 33 alias = generateRootAlias( persister.getEntityName() ); 34 } 35 36 protected final void initAll( 37 final String whereString, 38 final String orderByString, 39 final LockMode lockMode) 40 throws MappingException { 41 42 walkEntityTree( persister, getAlias() ); 43 44 List allAssociations = new ArrayList (); 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 projectionString, 62 final String whereString, 63 final String orderByString, 64 final String 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 condition, 74 final String orderBy, 75 final LockMode lockMode) 76 throws MappingException { 77 initStatementString(null, condition, orderBy, "", lockMode); 78 } 79 80 private void initStatementString( 81 final String projection, 82 final String condition, 83 final String orderBy, 84 final String 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 121 protected String getWhereFragment() throws MappingException { 122 return persister.whereJoinFragment(alias, true, true); 123 } 124 125 128 protected boolean isJoinedFetchEnabled(AssociationType type, FetchMode config) { 129 return isJoinedFetchEnabledInMapping(config, type); 130 } 131 132 public abstract String getComment(); 133 134 protected final Loadable getPersister() { 135 return persister; 136 } 137 138 protected final String getAlias() { 139 return alias; 140 } 141 142 public String toString() { 143 return getClass().getName() + '(' + getPersister().getEntityName() + ')'; 144 } 145 } 146 | Popular Tags |