1 package org.hibernate.loader.criteria; 3 4 import java.util.ArrayList ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.Set ; 8 9 import org.hibernate.Criteria; 10 import org.hibernate.FetchMode; 11 import org.hibernate.HibernateException; 12 import org.hibernate.LockMode; 13 import org.hibernate.MappingException; 14 import org.hibernate.engine.SessionFactoryImplementor; 15 import org.hibernate.impl.CriteriaImpl; 16 import org.hibernate.loader.AbstractEntityJoinWalker; 17 import org.hibernate.persister.entity.Joinable; 18 import org.hibernate.persister.entity.OuterJoinLoadable; 19 import org.hibernate.persister.entity.Queryable; 20 import org.hibernate.sql.JoinFragment; 21 import org.hibernate.type.AssociationType; 22 import org.hibernate.type.Type; 23 import org.hibernate.type.TypeFactory; 24 import org.hibernate.util.ArrayHelper; 25 26 32 public class CriteriaJoinWalker extends AbstractEntityJoinWalker { 33 34 38 41 private final CriteriaQueryTranslator translator; 42 private final Set querySpaces; 43 private final Type[] resultTypes; 44 private final String [] userAliases; 47 private final List userAliasList = new ArrayList (); 48 49 public Type[] getResultTypes() { 50 return resultTypes; 51 } 52 53 public String [] getUserAliases() { 54 return userAliases; 55 } 56 57 public CriteriaJoinWalker( 58 final OuterJoinLoadable persister, 59 final CriteriaQueryTranslator translator, 60 final SessionFactoryImplementor factory, 61 final CriteriaImpl criteria, 62 final String rootEntityName, 63 final Map enabledFilters) 64 throws HibernateException { 65 super(persister, factory, enabledFilters); 66 67 this.translator = translator; 68 69 querySpaces = translator.getQuerySpaces(); 70 71 if ( translator.hasProjection() ) { 72 resultTypes = translator.getProjectedTypes(); 73 74 initProjection( 75 translator.getSelect(), 76 translator.getWhereCondition(), 77 translator.getOrderBy(), 78 translator.getGroupBy(), 79 LockMode.NONE 80 ); 81 } 82 else { 83 resultTypes = new Type[] { TypeFactory.manyToOne( persister.getEntityName() ) }; 84 85 initAll( translator.getWhereCondition(), translator.getOrderBy(), LockMode.NONE ); 86 } 87 88 userAliasList.add( criteria.getAlias() ); userAliases = ArrayHelper.toStringArray(userAliasList); 90 91 } 92 93 protected int getJoinType( 94 AssociationType type, 95 FetchMode config, 96 String path, 97 String lhsTable, 98 String [] lhsColumns, 99 boolean nullable, 100 int currentDepth) 101 throws MappingException { 102 103 if ( translator.isJoin(path) ) { 104 return JoinFragment.INNER_JOIN; 105 } 106 else { 107 if ( translator.hasProjection() ) { 108 return -1; 109 } 110 else { 111 FetchMode fetchMode = translator.getRootCriteria() 112 .getFetchMode(path); 113 if ( isDefaultFetchMode(fetchMode) ) { 114 return super.getJoinType( 115 type, 116 config, 117 path, 118 lhsTable, 119 lhsColumns, 120 nullable, 121 currentDepth 122 ); 123 } 124 else { 125 if ( fetchMode==FetchMode.JOIN ) { 126 isDuplicateAssociation(lhsTable, lhsColumns, type); return getJoinType(nullable, currentDepth); 128 } 129 else { 130 return -1; 131 } 132 } 133 } 134 } 135 } 136 137 private static boolean isDefaultFetchMode(FetchMode fetchMode) { 138 return fetchMode==null || fetchMode==FetchMode.DEFAULT; 139 } 140 141 145 protected String getWhereFragment() throws MappingException { 146 return super.getWhereFragment() + 147 ( (Queryable) getPersister() ).filterFragment( getAlias(), getEnabledFilters() ); 148 } 149 150 protected String generateTableAlias(int n, String path, Joinable joinable) { 151 if ( joinable.consumesEntityAlias() ) { 152 final Criteria subcriteria = translator.getCriteria(path); 153 String sqlAlias = subcriteria==null ? null : translator.getSQLAlias(subcriteria); 154 if (sqlAlias!=null) { 155 userAliasList.add( subcriteria.getAlias() ); return sqlAlias; } 158 else { 159 userAliasList.add(null); 160 } 161 } 162 return super.generateTableAlias( n + translator.getSQLAliasCount(), path, joinable ); 163 } 164 165 protected String generateRootAlias(String tableName) { 166 return CriteriaQueryTranslator.ROOT_SQL_ALIAS; 167 } 168 169 public Set getQuerySpaces() { 170 return querySpaces; 171 } 172 173 public String getComment() { 174 return "criteria query"; 175 } 176 177 } 178 | Popular Tags |