Class Loader

  • Direct Known Subclasses:
    BasicLoader, CustomLoader

    public abstract class Loader
    extends java.lang.Object
    Abstract superclass of object loading (and querying) strategies. This class implements useful common functionality that concrete loaders delegate to. It is not intended that this functionality would be directly accessed by client code. (Hence, all methods of this class are declared protected or private.) This class relies heavily upon the Loadable interface, which is the contract between this class and EntityPersisters that may be loaded by it.

    The present implementation is able to load any number of columns of entities and at most one collection role per query.
    See Also:
    Loadable
    • Field Detail

      • LOG

        protected static final org.hibernate.internal.CoreMessageLogger LOG
    • Method Detail

      • getSQLString

        public abstract java.lang.String getSQLString()
        The SQL query string to be called; implemented by all subclasses
        Returns:
        The sql command this loader should use to get its ResultSet.
      • getEntityPersisters

        protected abstract Loadable[] getEntityPersisters()
        An array of persisters of entity classes contained in each row of results; implemented by all subclasses
        Returns:
        The entity persisters.
      • getEntityEagerPropertyFetches

        protected boolean[] getEntityEagerPropertyFetches()
        An array indicating whether the entities have eager property fetching enabled for all of their properties.

        Supersedes getEntityEagerPerPropertyFetches().

        Returns:
        Eager property fetching indicators.
      • getEntityEagerPerPropertyFetches

        protected boolean[][] getEntityEagerPerPropertyFetches()
        An array indicating for each entity which specific properties must have eager fetching enabled.

        Superseded by getEntityEagerPropertyFetches().

        Returns:
        Eager property fetching indicators.
      • getOwners

        protected int[] getOwners()
        An array of indexes of the entity that owns a one-to-one association to the entity at the given index (-1 if there is no "owner"). The indexes contained here are relative to the result of getEntityPersisters().
        Returns:
        The owner indicators (see discussion above).
      • getOwnerAssociationTypes

        protected EntityType[] getOwnerAssociationTypes()
        An array of the owner types corresponding to the getOwners() returns. Indices indicating no owner would be null here.
        Returns:
        The types for the owners.
      • getCollectionPersisters

        protected CollectionPersister[] getCollectionPersisters()
        An (optional) persister for a collection to be initialized; only collection loaders return a non-null value
      • getCollectionOwners

        protected int[] getCollectionOwners()
        Get the index of the entity that owns the collection, or -1 if there is no owner in the query results (ie. in the case of a collection initializer) or no collection.
      • getCompositeKeyManyToOneTargetIndices

        protected int[][] getCompositeKeyManyToOneTargetIndices()
      • getLockModes

        protected abstract LockMode[] getLockModes​(LockOptions lockOptions)
        What lock options does this load entities with?
        Parameters:
        lockOptions - a collection of lock options specified dynamically via the Query interface
      • upgradeLocks

        protected boolean upgradeLocks()
        Does this query return objects that might be already cached by the session, whose lock mode may need upgrading
      • isSingleRowLoader

        protected boolean isSingleRowLoader()
        Return false is this loader is a batch entity loader
      • getAliases

        protected java.lang.String[] getAliases()
        Get the SQL table aliases of entities whose associations are subselect-loadable, returning null if this loader does not support subselect loading
      • determineFollowOnLockMode

        protected LockMode determineFollowOnLockMode​(LockOptions lockOptions)
      • doQueryAndInitializeNonLazyCollections

        public java.util.List doQueryAndInitializeNonLazyCollections​(SharedSessionContractImplementor session,
                                                                     QueryParameters queryParameters,
                                                                     boolean returnProxies)
                                                              throws HibernateException,
                                                                     java.sql.SQLException
        Execute an SQL query and attempt to instantiate instances of the class mapped by the given persister from each row of the ResultSet. If an object is supplied, will attempt to initialize that object. If a collection is supplied, attempt to initialize that collection.
        Throws:
        HibernateException
        java.sql.SQLException
      • loadSingleRow

        public java.lang.Object loadSingleRow​(java.sql.ResultSet resultSet,
                                              SharedSessionContractImplementor session,
                                              QueryParameters queryParameters,
                                              boolean returnProxies)
                                       throws HibernateException
        Loads a single row from the result set. This is the processing used from the ScrollableResults where no collection fetches were encountered.
        Parameters:
        resultSet - The result set from which to do the load.
        session - The session from which the request originated.
        queryParameters - The query parameters specified by the user.
        returnProxies - Should proxies be generated
        Returns:
        The loaded "row".
        Throws:
        HibernateException
      • loadSequentialRowsForward

        public java.lang.Object loadSequentialRowsForward​(java.sql.ResultSet resultSet,
                                                          SharedSessionContractImplementor session,
                                                          QueryParameters queryParameters,
                                                          boolean returnProxies)
                                                   throws HibernateException
        Loads a single logical row from the result set moving forward. This is the processing used from the ScrollableResults where there were collection fetches encountered; thus a single logical row may have multiple rows in the underlying result set.
        Parameters:
        resultSet - The result set from which to do the load.
        session - The session from which the request originated.
        queryParameters - The query parameters specified by the user.
        returnProxies - Should proxies be generated
        Returns:
        The loaded "row".
        Throws:
        HibernateException
      • loadSequentialRowsReverse

        public java.lang.Object loadSequentialRowsReverse​(java.sql.ResultSet resultSet,
                                                          SharedSessionContractImplementor session,
                                                          QueryParameters queryParameters,
                                                          boolean returnProxies,
                                                          boolean isLogicallyAfterLast)
                                                   throws HibernateException
        Loads a single logical row from the result set moving forward. This is the processing used from the ScrollableResults where there were collection fetches encountered; thus a single logical row may have multiple rows in the underlying result set.
        Parameters:
        resultSet - The result set from which to do the load.
        session - The session from which the request originated.
        queryParameters - The query parameters specified by the user.
        returnProxies - Should proxies be generated
        Returns:
        The loaded "row".
        Throws:
        HibernateException
      • getRowsFromResultSet

        protected java.util.List<java.lang.Object> getRowsFromResultSet​(java.sql.ResultSet rs,
                                                                        QueryParameters queryParameters,
                                                                        SharedSessionContractImplementor session,
                                                                        boolean returnProxies,
                                                                        ResultTransformer forcedResultTransformer,
                                                                        int maxRows,
                                                                        java.util.List<java.lang.Object> hydratedObjects,
                                                                        java.util.List<EntityKey[]> subselectResultKeys)
                                                                 throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • isSubselectLoadingEnabled

        protected boolean isSubselectLoadingEnabled()
      • hasSubselectLoadableCollections

        protected boolean hasSubselectLoadableCollections()
      • resolveResultTransformer

        protected ResultTransformer resolveResultTransformer​(ResultTransformer resultTransformer)
        Determine the actual ResultTransformer that will be used to transform query results.
        Parameters:
        resultTransformer - the specified result transformer
        Returns:
        the actual result transformer
      • areResultSetRowsTransformedImmediately

        protected boolean areResultSetRowsTransformedImmediately()
        Are rows transformed immediately after being read from the ResultSet?
        Returns:
        true, if getResultColumnOrRow() transforms the results; false, otherwise
      • getResultRowAliases

        protected java.lang.String[] getResultRowAliases()
        Returns the aliases that corresponding to a result row.
        Returns:
        Returns the aliases that corresponding to a result row.
      • getResultColumnOrRow

        protected java.lang.Object getResultColumnOrRow​(java.lang.Object[] row,
                                                        ResultTransformer transformer,
                                                        java.sql.ResultSet rs,
                                                        SharedSessionContractImplementor session)
                                                 throws java.sql.SQLException,
                                                        HibernateException
        Get the actual object that is returned in the user-visible result list. This empty implementation merely returns its first argument. This is overridden by some subclasses.
        Throws:
        java.sql.SQLException
        HibernateException
      • includeInResultRow

        protected boolean[] includeInResultRow()
      • registerNonExists

        protected void registerNonExists​(EntityKey[] keys,
                                         Loadable[] persisters,
                                         SharedSessionContractImplementor session)
        For missing objects associated by one-to-one with another object in the result set, register the fact that the the object is missing with the session.
      • handleEmptyCollections

        protected void handleEmptyCollections​(java.io.Serializable[] keys,
                                              java.lang.Object resultSetId,
                                              SharedSessionContractImplementor session)
        If this is a collection initializer, we need to tell the session that a collection is being initialized, to account for the possibility of the collection having no elements (hence no rows in the result set).
      • getLimitHandler

        protected LimitHandler getLimitHandler​(RowSelection selection)
        Build LIMIT clause handler applicable for given selection criteria. Returns NoopLimitHandler delegate if dialect does not support LIMIT expression or processed query does not use pagination.
        Parameters:
        selection - Selection criteria.
        Returns:
        LIMIT clause delegate.
      • executeQueryStatement

        protected Loader.SqlStatementWrapper executeQueryStatement​(QueryParameters queryParameters,
                                                                   boolean scroll,
                                                                   java.util.List<AfterLoadAction> afterLoadActions,
                                                                   SharedSessionContractImplementor session)
                                                            throws java.sql.SQLException
        Process query string by applying filters, LIMIT clause, locks and comments if necessary. Finally execute SQL statement and advance to the first row.
        Throws:
        java.sql.SQLException
      • bindParameterValues

        protected int bindParameterValues​(java.sql.PreparedStatement statement,
                                          QueryParameters queryParameters,
                                          int startIndex,
                                          SharedSessionContractImplementor session)
                                   throws java.sql.SQLException
        Bind all parameter values into the prepared statement in preparation for execution.
        Parameters:
        statement - The JDBC prepared statement
        queryParameters - The encapsulation of the parameter values to be bound.
        startIndex - The position from which to start binding parameter values.
        session - The originating session.
        Returns:
        The number of JDBC bind positions actually bound during this method execution.
        Throws:
        java.sql.SQLException - Indicates problems performing the binding.
      • bindPositionalParameters

        protected int bindPositionalParameters​(java.sql.PreparedStatement statement,
                                               QueryParameters queryParameters,
                                               int startIndex,
                                               SharedSessionContractImplementor session)
                                        throws java.sql.SQLException,
                                               HibernateException
        Bind positional parameter values to the JDBC prepared statement.

        Positional parameters are those specified by JDBC-style ? parameters in the source query. It is (currently) expected that these come before any named parameters in the source query.

        Parameters:
        statement - The JDBC prepared statement
        queryParameters - The encapsulation of the parameter values to be bound.
        startIndex - The position from which to start binding parameter values.
        session - The originating session.
        Returns:
        The number of JDBC bind positions actually bound during this method execution.
        Throws:
        java.sql.SQLException - Indicates problems performing the binding.
        HibernateException - Indicates problems delegating binding to the types.
      • bindNamedParameters

        protected int bindNamedParameters​(java.sql.PreparedStatement statement,
                                          java.util.Map<java.lang.String,​TypedValue> namedParams,
                                          int startIndex,
                                          SharedSessionContractImplementor session)
                                   throws java.sql.SQLException,
                                          HibernateException
        Bind named parameters to the JDBC prepared statement.

        This is a generic implementation, the problem being that in the general case we do not know enough information about the named parameters to perform this in a complete manner here. Thus this is generally overridden on subclasses allowing named parameters to apply the specific behavior. The most usual limitation here is that we need to assume the type span is always one...

        Parameters:
        statement - The JDBC prepared statement
        namedParams - A map of parameter names to values
        startIndex - The position from which to start binding parameter values.
        session - The originating session.
        Returns:
        The number of JDBC bind positions actually bound during this method execution.
        Throws:
        java.sql.SQLException - Indicates problems performing the binding.
        HibernateException - Indicates problems delegating binding to the types.
      • getNamedParameterLocs

        public int[] getNamedParameterLocs​(java.lang.String name)
      • autoDiscoverTypes

        protected void autoDiscoverTypes​(java.sql.ResultSet rs)
      • checkScrollability

        protected void checkScrollability()
                                   throws HibernateException
        Check whether the current loader can support returning ScrollableResults.
        Throws:
        HibernateException
      • needsFetchingScroll

        protected boolean needsFetchingScroll()
        Does the result set to be scrolled contain collection fetches?
        Returns:
        True if it does, and thus needs the special fetching scroll functionality; false otherwise.
      • scroll

        protected ScrollableResultsImplementor scroll​(QueryParameters queryParameters,
                                                      Type[] returnTypes,
                                                      org.hibernate.hql.internal.HolderInstantiator holderInstantiator,
                                                      SharedSessionContractImplementor session)
                                               throws HibernateException
        Return the query results, as an instance of ScrollableResults
        Parameters:
        queryParameters - The parameters with which the query should be executed.
        returnTypes - The expected return types of the query
        holderInstantiator - If the return values are expected to be wrapped in a holder, this is the thing that knows how to wrap them.
        session - The session from which the scroll request originated.
        Returns:
        The ScrollableResults instance.
        Throws:
        HibernateException - Indicates an error executing the query, or constructing the ScrollableResults.
      • postInstantiate

        protected void postInstantiate()
        Calculate and cache select-clause suffixes. Must be called by subclasses after instantiation.
      • getEntityAliases

        protected abstract EntityAliases[] getEntityAliases()
        Get the result set descriptor
      • getCollectionAliases

        protected abstract CollectionAliases[] getCollectionAliases()
      • getQueryIdentifier

        protected java.lang.String getQueryIdentifier()
        Identifies the query for statistics reporting, if null, no statistics will be reported
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • processDistinctKeyword

        protected java.lang.String processDistinctKeyword​(java.lang.String sql,
                                                          QueryParameters parameters)
        Remove distinct keyword from SQL statement if the query should not pass it through.
        Parameters:
        sql - SQL string
        parameters - SQL parameters
        Returns:
        SQL string