1 package org.hibernate.loader.criteria; 3 4 import java.sql.ResultSet ; 5 import java.sql.SQLException ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 import org.hibernate.HibernateException; 12 import org.hibernate.LockMode; 13 import org.hibernate.QueryException; 14 import org.hibernate.ScrollMode; 15 import org.hibernate.ScrollableResults; 16 import org.hibernate.dialect.Dialect; 17 import org.hibernate.engine.QueryParameters; 18 import org.hibernate.engine.SessionFactoryImplementor; 19 import org.hibernate.engine.SessionImplementor; 20 import org.hibernate.impl.CriteriaImpl; 21 import org.hibernate.loader.OuterJoinLoader; 22 import org.hibernate.persister.entity.Loadable; 23 import org.hibernate.persister.entity.OuterJoinLoadable; 24 import org.hibernate.sql.ForUpdateFragment; 25 import org.hibernate.type.Type; 26 27 33 public class CriteriaLoader extends OuterJoinLoader { 34 35 39 42 private final CriteriaQueryTranslator translator; 43 private final Set querySpaces; 44 private final Type[] resultTypes; 45 private final String [] userAliases; 48 49 public CriteriaLoader( 50 final OuterJoinLoadable persister, 51 final SessionFactoryImplementor factory, 52 final CriteriaImpl criteria, 53 final String rootEntityName, 54 final Map enabledFilters) 55 throws HibernateException { 56 super(factory, enabledFilters); 57 58 translator = new CriteriaQueryTranslator( 59 factory, 60 criteria, 61 rootEntityName, 62 CriteriaQueryTranslator.ROOT_SQL_ALIAS 63 ); 64 65 querySpaces = translator.getQuerySpaces(); 66 67 CriteriaJoinWalker walker = new CriteriaJoinWalker( 68 persister, 69 translator, 70 factory, 71 criteria, 72 rootEntityName, 73 enabledFilters 74 ); 75 76 initFromWalker(walker); 77 78 userAliases = walker.getUserAliases(); 79 resultTypes = walker.getResultTypes(); 80 81 postInstantiate(); 82 83 } 84 85 public ScrollableResults scroll(SessionImplementor session, ScrollMode scrollMode) 86 throws HibernateException { 87 QueryParameters qp = translator.getQueryParameters(); 88 qp.setScrollMode(scrollMode); 89 return scroll(qp, resultTypes, null, session); 90 } 91 92 public List list(SessionImplementor session) 93 throws HibernateException { 94 return list( session, translator.getQueryParameters(), querySpaces, resultTypes ); 95 } 96 97 protected Object getResultColumnOrRow(Object [] row, ResultSet rs, SessionImplementor session) 98 throws SQLException , HibernateException { 99 final Object [] result; 100 final String [] aliases; 101 if ( translator.hasProjection() ) { 102 Type[] types = translator.getProjectedTypes(); 103 result = new Object [types.length]; 104 String [] columnAliases = translator.getProjectedColumnAliases(); 105 for ( int i=0; i<result.length; i++ ) { 106 result[i] = types[i].nullSafeGet(rs, columnAliases[i], session, null); 107 } 108 aliases = translator.getProjectedAliases(); 109 } 110 else { 111 result = row; 112 aliases = userAliases; 113 } 114 return translator.getRootCriteria().getResultTransformer().transformTuple(result, aliases); 115 } 116 117 public Set getQuerySpaces() { 118 return querySpaces; 119 } 120 121 protected String applyLocks(String sqlSelectString, Map lockModes, Dialect dialect) 122 throws QueryException { 123 124 if ( lockModes==null || lockModes.size()==0 ) { 125 return sqlSelectString; 126 } 127 else { 128 Map keyColumnNames = null; 129 Loadable[] persisters = getEntityPersisters(); 130 String [] entityAliases = getAliases(); 131 if ( dialect.forUpdateOfColumns() ) { 132 keyColumnNames = new HashMap (); 133 for ( int i=0; i<entityAliases.length; i++ ) { 134 keyColumnNames.put( entityAliases[i], persisters[i].getIdentifierColumnNames() ); 135 } 136 } 137 return sqlSelectString + 138 new ForUpdateFragment(dialect, lockModes, keyColumnNames).toFragmentString(); 139 } 140 } 141 142 protected LockMode[] getLockModes(Map lockModes) { 143 final String [] entityAliases = getAliases(); 144 if (entityAliases==null) return null; 145 final int size = entityAliases.length; 146 LockMode[] lockModesArray = new LockMode[size]; 147 for ( int i=0; i<size; i++ ) { 148 LockMode lockMode = (LockMode) lockModes.get( entityAliases[i] ); 149 lockModesArray[i] = lockMode==null ? LockMode.NONE : lockMode; 150 } 151 return lockModesArray; 152 } 153 154 protected boolean isSubselectLoadingEnabled() { 155 return hasSubselectLoadableCollections(); 156 } 157 158 protected List getResultList(List results) { 159 return translator.getRootCriteria().getResultTransformer().transformList(results); 160 } 161 162 } 163 | Popular Tags |