private ResultSet processResultSet( ResultSet rs, final RowSelection selection, final LimitHandler limitHandler, final boolean autodiscovertypes, final SharedSessionContractImplementor session ) throws SQLException, HibernateException { rs = wrapResultSetIfEnabled( rs, session ); if ( !limitHandler.supportsLimitOffset() || !LimitHelper.useLimit( limitHandler, selection ) ) { advance( rs, selection ); } if ( autodiscovertypes ) { autoDiscoverTypes( rs ); } return rs; }
/** * Modify the SQL, adding lock hints and comments, if necessary */ protected String preprocessSQL( String sql, QueryParameters parameters, SessionFactoryImplementor sessionFactory, List<AfterLoadAction> afterLoadActions) throws HibernateException { Dialect dialect = sessionFactory.getServiceRegistry().getService( JdbcServices.class ).getDialect(); sql = applyLocks( sql, parameters, dialect, afterLoadActions ); sql = dialect.addSqlHintOrComment( sql, parameters, sessionFactory.getSessionFactoryOptions().isCommentsEnabled() ); return processDistinctKeyword( sql, parameters ); }
/** * Bind all parameter values into the prepared statement in preparation * for execution. * * @param statement The JDBC prepared statement * @param queryParameters The encapsulation of the parameter values to be bound. * @param startIndex The position from which to start binding parameter values. * @param session The originating session. * * @return The number of JDBC bind positions actually bound during this method execution. * * @throws SQLException Indicates problems performing the binding. */ protected int bindParameterValues( PreparedStatement statement, QueryParameters queryParameters, int startIndex, SharedSessionContractImplementor session) throws SQLException { int span = 0; span += bindPositionalParameters( statement, queryParameters, startIndex, session ); span += bindNamedParameters( statement, queryParameters.getNamedParameters(), startIndex + span, session ); return span; }
int maxRows, List<AfterLoadAction> afterLoadActions) throws SQLException { final int entitySpan = getEntityPersisters().length; final EntityKey optionalObjectKey = getOptionalObjectKey( queryParameters, session ); final LockMode[] lockModesArray = getLockModes( queryParameters.getLockOptions() ); final boolean createSubselects = isSubselectLoadingEnabled(); final List subselectResultKeys = createSubselects ? new ArrayList() : null; final ArrayList hydratedObjects = entitySpan == 0 ? null : new ArrayList( entitySpan * 10 ); final List results = new ArrayList(); handleEmptyCollections( queryParameters.getCollectionKeys(), rs, session ); EntityKey[] keys = new EntityKey[entitySpan]; //we can reuse it for each row LOG.trace( "Processing result set" ); LOG.debugf( "Result set row: %s", count ); Object result = getRowFromResultSet( rs, session, initializeEntitiesAndCollections( hydratedObjects, rs, ); if ( createSubselects ) { createSubselects( subselectResultKeys, queryParameters, session );
final int maxRows = hasMaxRows( selection ) ? selection.getMaxRows().intValue() : Integer.MAX_VALUE; final int entitySpan = getEntityPersisters().length; final PreparedStatement st = prepareQueryStatement( queryParameters, false, session ); final ResultSet rs = getResultSet( st, queryParameters.isCallable(), selection, session ); final LockMode[] lockModeArray = getLockModes( queryParameters.getLockModes() ); final EntityKey optionalObjectKey = getOptionalObjectKey( queryParameters, session ); final boolean createSubselects = isSubselectLoadingEnabled(); final List subselectResultKeys = createSubselects ? new ArrayList() : null; handleEmptyCollections( queryParameters.getCollectionKeys(), rs, session ); Object result = getRowFromResultSet( rs, session, queryParameters, initializeEntitiesAndCollections( hydratedObjects, rs, session, queryParameters.isReadOnly() ); if ( createSubselects ) createSubselects( subselectResultKeys, queryParameters, session );
final EntityKey[] keys, boolean returnProxies) throws SQLException, HibernateException { return getRowFromResultSet( resultSet, session, boolean returnProxies, ResultTransformer forcedResultTransformer) throws SQLException, HibernateException { final Loadable[] persisters = getEntityPersisters(); final int entitySpan = persisters.length; extractKeysFromResultSet( persisters, queryParameters, registerNonExists( keys, persisters, session ); Object[] row = getRow( resultSet, persisters, readCollectionElements( row, resultSet, session ); applyPostLoadLocks( row, lockModesArray, session ); ? getResultColumnOrRow( row, queryParameters.getResultTransformer(), resultSet, session ) : forcedResultTransformer.transformTuple( getResultRow( row, resultSet, session ), getResultRowAliases()
/** * Called by subclasses that batch initialize collections */ protected final void loadCollectionSubselect( final SharedSessionContractImplementor session, final Serializable[] ids, final Object[] parameterValues, final Type[] parameterTypes, final Map<String, TypedValue> namedParameters, final Type type) throws HibernateException { final Type[] idTypes = new Type[ids.length]; Arrays.fill( idTypes, type ); try { doQueryAndInitializeNonLazyCollections( session, new QueryParameters( parameterTypes, parameterValues, namedParameters, ids ), true ); } catch (SQLException sqle) { throw factory.getJdbcServices().getSqlExceptionHelper().convert( sqle, "could not load collection by subselect: " + MessageHelper.collectionInfoString( getCollectionPersisters()[0], ids, getFactory() ), getSQLString() ); } }
throws HibernateException { final boolean stats = getFactory().getStatistics().isStatisticsEnabled(); long startTime = 0; if ( stats ) { result = doQueryAndInitializeNonLazyCollections( session, queryParameters, true, forcedResultTransformer ); sqle, "could not execute query", getSQLString() ); final long endTime = System.nanoTime(); final long milliseconds = TimeUnit.MILLISECONDS.convert( endTime - startTime, TimeUnit.NANOSECONDS ); getFactory().getStatistics().queryExecuted( getQueryIdentifier(), result.size(), milliseconds
LockOptions lockOptions) throws HibernateException { if ( LOG.isDebugEnabled() ) { LOG.debugf( "Batch loading entity: %s", MessageHelper.infoString( persister, ids, getFactory() ) ); qp.setOptionalId( optionalId ); qp.setLockOptions( lockOptions ); result = doQueryAndInitializeNonLazyCollections( session, qp, false ); sqle, "could not load an entity batch: " + MessageHelper.infoString( getEntityPersisters()[0], ids, getFactory() ), getSQLString() );
Dialect dialect = getFactory().getDialect(); if (callable) { rs = session.getBatcher().getResultSet( (CallableStatement) st, dialect ); rs = session.getBatcher().getResultSet( st ); rs = wrapResultSetIfEnabled( rs , session ); advance( rs, selection ); autoDiscoverTypes( rs );
queryParameters.processFilters( getSQLString(), session ); String sql = queryParameters.getFilteredSQL(); final Dialect dialect = getFactory().getDialect(); final RowSelection selection = queryParameters.getRowSelection(); boolean useLimit = useLimit( selection, dialect ); boolean hasFirstRow = getFirstRow( selection ) > 0; boolean useOffset = hasFirstRow && useLimit && dialect.supportsLimitOffset(); boolean callable = queryParameters.isCallable(); getFactory().getSettings().isScrollableResultSetsEnabled(); ScrollMode scrollMode = scroll ? queryParameters.getScrollMode() : ScrollMode.SCROLL_INSENSITIVE; useOffset ? getFirstRow(selection) : 0, getMaxOrLimit(selection, dialect) ); sql = preprocessSQL( sql, queryParameters, dialect ); col += bindLimitParameters( st, col, selection ); col += bindPositionalParameters( st, queryParameters, col, session ); col += bindNamedParameters( st, queryParameters.getNamedParameters(), col, session ); col += bindLimitParameters( st, col, selection ); if ( !useLimit ) setMaxRows( st, selection ); if ( selection != null ) { if ( selection.getTimeout() != null ) {
private void createSubselects(List keys, QueryParameters queryParameters, SharedSessionContractImplementor session) { if ( keys.size() > 1 ) { //if we only returned one entity, query by key is more efficient Set[] keySets = transpose( keys ); Map namedParameterLocMap = buildNamedParameterLocMap( queryParameters ); final Loadable[] loadables = getEntityPersisters(); final String[] aliases = getAliases(); final String subselectQueryString = SubselectFetch.createSubselectFetchQueryFragment( queryParameters ); for ( Object key : keys ) { final EntityKey[] rowKeys = (EntityKey[]) key; for ( int i = 0; i < rowKeys.length; i++ ) { if ( rowKeys[i] != null && loadables[i].hasSubselectLoadableCollections() ) { SubselectFetch subselectFetch = new SubselectFetch( subselectQueryString, aliases[i], loadables[i], queryParameters, keySets[i], namedParameterLocMap ); session.getPersistenceContext() .getBatchFetchQueue() .addSubselect( rowKeys[i], subselectFetch ); } } } } }
/** * Modify the SQL, adding lock hints and comments, if necessary */ protected String preprocessSQL(String sql, QueryParameters parameters, Dialect dialect) throws HibernateException { sql = applyLocks( sql, parameters.getLockOptions(), dialect ); return getFactory().getSettings().isCommentsEnabled() ? prependComment( sql, parameters ) : sql; }
result = doQueryAndInitializeNonLazyCollections( session, new QueryParameters( sqle, "could not load collection element by index", getSQLString() );
if ( LockMode.NONE != requestedLockMode && upgradeLocks() ) { //no point doing this if NONE was requested final EntityEntry entry = session.getPersistenceContext().getEntry( object ); if ( entry.getLockMode().lessThan( requestedLockMode ) ) { checkVersion( i, persister, key.getIdentifier(), object, rs, session );
List<AfterLoadAction> afterLoadActions) throws HibernateException { final CollectionPersister[] collectionPersisters = getCollectionPersisters(); if ( collectionPersisters != null ) { for ( CollectionPersister collectionPersister : collectionPersisters ) { endCollectionLoad( resultSetId, session, collectionPersister ); endCollectionLoad( resultSetId, session, collectionPersister );
private void createSubselects(List keys, QueryParameters queryParameters, SessionImplementor session) { if ( keys.size() > 1 ) { //if we only returned one entity, query by key is more efficient Set[] keySets = transpose(keys); final Loadable[] loadables = getEntityPersisters(); final String[] aliases = getAliases(); final Iterator iter = keys.iterator(); while ( iter.hasNext() ) {
final PostLoadEvent post = new PostLoadEvent(session); final CollectionPersister[] collectionPersisters = getCollectionPersisters(); if ( collectionPersisters != null ) { for ( int i=0; i<collectionPersisters.length; i++ ) { endCollectionLoad( resultSetId, session, collectionPersisters[i] ); if ( getEntityPersisters().length > 0 ) { //if no persisters, hydratedObjects is null int hydratedObjectsSize = hydratedObjects.size(); if ( log.isTraceEnabled() ) log.trace( "total objects hydrated: " + hydratedObjectsSize ); endCollectionLoad( resultSetId, session, collectionPersisters[i] );
try { try { result = doQuery( session, queryParameters, returnProxies, forcedResultTransformer );
/** * Execute an SQL query and attempt to instantiate instances of the class mapped by the given * persister from each row of the <tt>ResultSet</tt>. If an object is supplied, will attempt to * initialize that object. If a collection is supplied, attempt to initialize that collection. */ public List doQueryAndInitializeNonLazyCollections( final SharedSessionContractImplementor session, final QueryParameters queryParameters, final boolean returnProxies) throws HibernateException, SQLException { return doQueryAndInitializeNonLazyCollections( session, queryParameters, returnProxies, null ); }