public abstract class Loader extends 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 <tt>protected</tt> or <tt>private</tt>.) This class relies heavily upon the <tt>Loadable</tt> interface, which is the contract between this class and <tt>EntityPersister</tt>s that may be loaded by it.<br> <br> The present implementation is able to load any number of columns of entities and at most one collection role per query.
Loadable
Modifier and Type | Class and Description |
---|---|
protected static class |
Loader.SqlStatementWrapper
|
Modifier and Type | Field and Description |
---|---|
protected static boolean |
DEBUG_ENABLED |
protected static org.hibernate.internal.CoreMessageLogger |
LOG |
static String |
SELECT |
static String |
SELECT_DISTINCT |
Constructor and Description |
---|
Loader(SessionFactoryImplementor factory) |
Modifier and Type | Method and Description |
---|---|
protected String |
applyLocks(String sql,
QueryParameters parameters,
Dialect dialect,
List<AfterLoadAction> afterLoadActions)
Append <tt>FOR UPDATE OF</tt> clause, if necessary.
|
protected void |
applyPostLoadLocks(Object[] row,
LockMode[] lockModesArray,
SharedSessionContractImplementor session) |
protected boolean |
areResultSetRowsTransformedImmediately()
Are rows transformed immediately after being read from the ResultSet?
|
protected void |
autoDiscoverTypes(ResultSet rs) |
protected int |
bindNamedParameters(PreparedStatement statement,
Map<String,TypedValue> namedParams,
int startIndex,
SharedSessionContractImplementor session)
Bind named parameters to the JDBC prepared statement.
|
protected int |
bindParameterValues(PreparedStatement statement,
QueryParameters queryParameters,
int startIndex,
SharedSessionContractImplementor session)
Bind all parameter values into the prepared statement in preparation
for execution.
|
protected int |
bindPositionalParameters(PreparedStatement statement,
QueryParameters queryParameters,
int startIndex,
SharedSessionContractImplementor session)
Bind positional parameter values to the JDBC prepared statement.
|
protected void |
checkScrollability()
Check whether the current loader can support returning ScrollableResults.
|
protected LockMode |
determineFollowOnLockMode(LockOptions lockOptions) |
protected List |
doList(SharedSessionContractImplementor session,
QueryParameters queryParameters)
Actually execute a query, ignoring the query cache
|
List |
doQueryAndInitializeNonLazyCollections(SharedSessionContractImplementor session,
QueryParameters queryParameters,
boolean returnProxies)
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>.
|
List |
doQueryAndInitializeNonLazyCollections(SharedSessionContractImplementor session,
QueryParameters queryParameters,
boolean returnProxies,
ResultTransformer forcedResultTransformer) |
protected Loader.SqlStatementWrapper |
executeQueryStatement(QueryParameters queryParameters,
boolean scroll,
List<AfterLoadAction> afterLoadActions,
SharedSessionContractImplementor session)
Process query string by applying filters, LIMIT clause, locks and comments if necessary.
|
protected Loader.SqlStatementWrapper |
executeQueryStatement(String sqlStatement,
QueryParameters queryParameters,
boolean scroll,
List<AfterLoadAction> afterLoadActions,
SharedSessionContractImplementor session) |
protected void |
extractKeysFromResultSet(Loadable[] persisters,
QueryParameters queryParameters,
ResultSet resultSet,
SharedSessionContractImplementor session,
EntityKey[] keys,
LockMode[] lockModes,
List hydratedObjects) |
protected String[] |
getAliases()
Get the SQL table aliases of entities whose
associations are subselect-loadable, returning
null if this loader does not support subselect
loading
|
protected abstract CollectionAliases[] |
getCollectionAliases() |
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.
|
protected CollectionPersister[] |
getCollectionPersisters()
An (optional) persister for a collection to be initialized; only
collection loaders return a non-null value
|
protected int[][] |
getCompositeKeyManyToOneTargetIndices() |
protected abstract EntityAliases[] |
getEntityAliases()
Get the result set descriptor
|
protected boolean[] |
getEntityEagerPropertyFetches()
An array indicating whether the entities have eager property fetching
enabled.
|
protected abstract Loadable[] |
getEntityPersisters()
An array of persisters of entity classes contained in each row of results;
implemented by all subclasses
|
SessionFactoryImplementor |
getFactory() |
protected LimitHandler |
getLimitHandler(RowSelection selection)
Build LIMIT clause handler applicable for given selection criteria.
|
protected abstract LockMode[] |
getLockModes(LockOptions lockOptions)
What lock options does this load entities with?
|
int[] |
getNamedParameterLocs(String name) |
protected EntityType[] |
getOwnerAssociationTypes()
An array of the owner types corresponding to the
getOwners()
returns. |
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").
|
protected String |
getQueryIdentifier()
Identifies the query for statistics reporting, if null,
no statistics will be reported
|
protected Object |
getResultColumnOrRow(Object[] row,
ResultTransformer transformer,
ResultSet rs,
SharedSessionContractImplementor session)
Get the actual object that is returned in the user-visible result list.
|
protected List |
getResultList(List results,
ResultTransformer resultTransformer) |
protected Object[] |
getResultRow(Object[] row,
ResultSet rs,
SharedSessionContractImplementor session) |
protected String[] |
getResultRowAliases()
Returns the aliases that corresponding to a result row.
|
protected ResultSet |
getResultSet(CallableStatement st,
RowSelection selection,
LimitHandler limitHandler,
boolean autodiscovertypes,
SharedSessionContractImplementor session)
Execute given <tt>CallableStatement</tt>, advance to the first result and return SQL <tt>ResultSet</tt>.
|
protected ResultSet |
getResultSet(PreparedStatement st,
RowSelection selection,
LimitHandler limitHandler,
boolean autodiscovertypes,
SharedSessionContractImplementor session)
Execute given <tt>PreparedStatement</tt>, advance to the first result and return SQL <tt>ResultSet</tt>.
|
abstract String |
getSQLString()
The SQL query string to be called; implemented by all subclasses
|
protected boolean |
hasSubselectLoadableCollections() |
protected boolean[] |
includeInResultRow() |
protected boolean |
isSingleRowLoader()
Return false is this loader is a batch entity loader
|
protected boolean |
isSubselectLoadingEnabled() |
protected List |
list(SharedSessionContractImplementor session,
QueryParameters queryParameters,
Set<Serializable> querySpaces,
Type[] resultTypes)
Return the query results, using the query cache, called
by subclasses that implement cacheable queries
|
void |
loadCollection(SharedSessionContractImplementor session,
Serializable id,
Type type)
Called by subclasses that initialize collections
|
void |
loadCollectionBatch(SharedSessionContractImplementor session,
Serializable[] ids,
Type type)
Called by wrappers that batch initialize collections
|
protected void |
loadCollectionSubselect(SharedSessionContractImplementor session,
Serializable[] ids,
Object[] parameterValues,
Type[] parameterTypes,
Map<String,TypedValue> namedParameters,
Type type)
Called by subclasses that batch initialize collections
|
protected List |
loadEntity(SharedSessionContractImplementor session,
Object key,
Object index,
Type keyType,
Type indexType,
EntityPersister persister)
Called by subclasses that load entities
|
protected List |
loadEntity(SharedSessionContractImplementor session,
Object id,
Type identifierType,
Object optionalObject,
String optionalEntityName,
Serializable optionalIdentifier,
EntityPersister persister,
LockOptions lockOptions)
Called by subclasses that load entities
|
List |
loadEntityBatch(SharedSessionContractImplementor session,
Serializable[] ids,
Type idType,
Object optionalObject,
String optionalEntityName,
Serializable optionalId,
EntityPersister persister,
LockOptions lockOptions)
Called by wrappers that batch load entities
|
Object |
loadSequentialRowsForward(ResultSet resultSet,
SharedSessionContractImplementor session,
QueryParameters queryParameters,
boolean returnProxies)
Loads a single logical row from the result set moving forward.
|
Object |
loadSequentialRowsReverse(ResultSet resultSet,
SharedSessionContractImplementor session,
QueryParameters queryParameters,
boolean returnProxies,
boolean isLogicallyAfterLast)
Loads a single logical row from the result set moving forward.
|
Object |
loadSingleRow(ResultSet resultSet,
SharedSessionContractImplementor session,
QueryParameters queryParameters,
boolean returnProxies)
Loads a single row from the result set.
|
protected boolean |
needsFetchingScroll()
Does the result set to be scrolled contain collection fetches?
|
protected void |
postInstantiate()
Calculate and cache select-clause suffixes.
|
protected PreparedStatement |
prepareQueryStatement(String sql,
QueryParameters queryParameters,
LimitHandler limitHandler,
boolean scroll,
SharedSessionContractImplementor session)
Obtain a <tt>PreparedStatement</tt> with all parameters pre-bound.
|
protected String |
preprocessSQL(String sql,
QueryParameters parameters,
SessionFactoryImplementor sessionFactory,
List<AfterLoadAction> afterLoadActions)
Modify the SQL, adding lock hints and comments, if necessary
|
protected String |
processDistinctKeyword(String sql,
QueryParameters parameters)
Remove distinct keyword from SQL statement if the query should not pass it through.
|
protected List |
processResultSet(ResultSet rs,
QueryParameters queryParameters,
SharedSessionContractImplementor session,
boolean returnProxies,
ResultTransformer forcedResultTransformer,
int maxRows,
List<AfterLoadAction> afterLoadActions) |
protected void |
putResultInQueryCache(SharedSessionContractImplementor session,
QueryParameters queryParameters,
Type[] resultTypes,
QueryResultsCache queryCache,
QueryKey key,
List result) |
protected ResultTransformer |
resolveResultTransformer(ResultTransformer resultTransformer)
Determine the actual ResultTransformer that will be used to
transform query results.
|
protected ScrollableResultsImplementor |
scroll(QueryParameters queryParameters,
Type[] returnTypes,
org.hibernate.hql.internal.HolderInstantiator holderInstantiator,
SharedSessionContractImplementor session)
Return the query results, as an instance of <tt>ScrollableResults</tt>
|
protected boolean |
shouldUseFollowOnLocking(QueryParameters parameters,
Dialect dialect,
List<AfterLoadAction> afterLoadActions) |
String |
toString() |
protected boolean |
upgradeLocks()
Does this query return objects that might be already cached
by the session, whose lock mode may need upgrading
|
public static final String SELECT
public static final String SELECT_DISTINCT
protected static final org.hibernate.internal.CoreMessageLogger LOG
protected static final boolean DEBUG_ENABLED
public Loader(SessionFactoryImplementor factory)
public abstract String getSQLString()
The SQL query string to be called; implemented by all subclasses
ResultSet
.protected abstract Loadable[] getEntityPersisters()
An array of persisters of entity classes contained in each row of results; implemented by all subclasses
protected boolean[] getEntityEagerPropertyFetches()
An array indicating whether the entities have eager property fetching enabled.
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()
.
protected EntityType[] getOwnerAssociationTypes()
An array of the owner types corresponding to the getOwners()
returns. Indices indicating no owner would be null here.
protected CollectionPersister[] getCollectionPersisters()
An (optional) persister for a collection to be initialized; only collection loaders return a non-null value
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.
protected int[][] getCompositeKeyManyToOneTargetIndices()
protected abstract LockMode[] getLockModes(LockOptions lockOptions)
What lock options does this load entities with?
lockOptions
- a collection of lock options specified dynamically via the Query interfaceprotected String applyLocks(String sql, QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) throws HibernateException
Append <tt>FOR UPDATE OF</tt> clause, if necessary. This empty superclass implementation merely returns its first argument.
HibernateException
protected boolean upgradeLocks()
Does this query return objects that might be already cached by the session, whose lock mode may need upgrading
protected boolean isSingleRowLoader()
Return false is this loader is a batch entity loader
protected String[] getAliases()
Get the SQL table aliases of entities whose associations are subselect-loadable, returning null if this loader does not support subselect loading
protected String preprocessSQL(String sql, QueryParameters parameters, SessionFactoryImplementor sessionFactory, List<AfterLoadAction> afterLoadActions) throws HibernateException
Modify the SQL, adding lock hints and comments, if necessary
HibernateException
protected boolean shouldUseFollowOnLocking(QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions)
protected LockMode determineFollowOnLockMode(LockOptions lockOptions)
public List doQueryAndInitializeNonLazyCollections(SharedSessionContractImplementor session, QueryParameters queryParameters, boolean returnProxies) throws HibernateException, SQLException
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.
HibernateException
SQLException
public List doQueryAndInitializeNonLazyCollections(SharedSessionContractImplementor session, QueryParameters queryParameters, boolean returnProxies, ResultTransformer forcedResultTransformer) throws HibernateException, SQLException
HibernateException
SQLException
public Object loadSingleRow(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.
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 generatedHibernateException
public Object loadSequentialRowsForward(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.
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 generatedHibernateException
public Object loadSequentialRowsReverse(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.
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 generatedHibernateException
protected void extractKeysFromResultSet(Loadable[] persisters, QueryParameters queryParameters, ResultSet resultSet, SharedSessionContractImplementor session, EntityKey[] keys, LockMode[] lockModes, List hydratedObjects) throws SQLException
SQLException
protected void applyPostLoadLocks(Object[] row, LockMode[] lockModesArray, SharedSessionContractImplementor session)
protected List processResultSet(ResultSet rs, QueryParameters queryParameters, SharedSessionContractImplementor session, boolean returnProxies, ResultTransformer forcedResultTransformer, int maxRows, List<AfterLoadAction> afterLoadActions) throws SQLException
SQLException
protected boolean isSubselectLoadingEnabled()
protected boolean hasSubselectLoadableCollections()
protected ResultTransformer resolveResultTransformer(ResultTransformer resultTransformer)
Determine the actual ResultTransformer that will be used to transform query results.
resultTransformer
- the specified result transformerprotected List getResultList(List results, ResultTransformer resultTransformer) throws QueryException
QueryException
protected boolean areResultSetRowsTransformedImmediately()
Are rows transformed immediately after being read from the ResultSet?
protected String[] getResultRowAliases()
Returns the aliases that corresponding to a result row.
protected Object getResultColumnOrRow(Object[] row, ResultTransformer transformer, ResultSet rs, SharedSessionContractImplementor session) throws 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.
SQLException
HibernateException
protected boolean[] includeInResultRow()
protected Object[] getResultRow(Object[] row, ResultSet rs, SharedSessionContractImplementor session) throws SQLException, HibernateException
SQLException
HibernateException
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.
selection
- Selection criteria.protected Loader.SqlStatementWrapper executeQueryStatement(QueryParameters queryParameters, boolean scroll, List<AfterLoadAction> afterLoadActions, SharedSessionContractImplementor session) throws SQLException
Process query string by applying filters, LIMIT clause, locks and comments if necessary. Finally execute SQL statement and advance to the first row.
SQLException
protected Loader.SqlStatementWrapper executeQueryStatement(String sqlStatement, QueryParameters queryParameters, boolean scroll, List<AfterLoadAction> afterLoadActions, SharedSessionContractImplementor session) throws SQLException
SQLException
protected final PreparedStatement prepareQueryStatement(String sql, QueryParameters queryParameters, LimitHandler limitHandler, boolean scroll, SharedSessionContractImplementor session) throws SQLException, HibernateException
Obtain a <tt>PreparedStatement</tt> with all parameters pre-bound. Bind JDBC-style <tt>?</tt> parameters, named parameters, and limit parameters.
SQLException
HibernateException
protected int bindParameterValues(PreparedStatement statement, QueryParameters queryParameters, int startIndex, SharedSessionContractImplementor session) throws SQLException
Bind all parameter values into the prepared statement in preparation for execution.
statement
- The JDBC prepared statementqueryParameters
- The encapsulation of the parameter values to be bound.startIndex
- The position from which to start binding parameter values.session
- The originating session.SQLException
- Indicates problems performing the binding.protected int bindPositionalParameters(PreparedStatement statement, QueryParameters queryParameters, int startIndex, SharedSessionContractImplementor session) throws SQLException, HibernateException
Bind positional parameter values to the JDBC prepared statement. <p/> 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.
statement
- The JDBC prepared statementqueryParameters
- The encapsulation of the parameter values to be bound.startIndex
- The position from which to start binding parameter values.session
- The originating session.SQLException
- Indicates problems performing the binding.HibernateException
- Indicates problems delegating binding to the types.protected int bindNamedParameters(PreparedStatement statement, Map<String,TypedValue> namedParams, int startIndex, SharedSessionContractImplementor session) throws SQLException, HibernateException
Bind named parameters to the JDBC prepared statement. <p/> 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…
statement
- The JDBC prepared statementnamedParams
- A map of parameter names to valuesstartIndex
- The position from which to start binding parameter values.session
- The originating session.SQLException
- Indicates problems performing the binding.HibernateException
- Indicates problems delegating binding to the types.public int[] getNamedParameterLocs(String name)
protected final ResultSet getResultSet(PreparedStatement st, RowSelection selection, LimitHandler limitHandler, boolean autodiscovertypes, SharedSessionContractImplementor session) throws SQLException, HibernateException
Execute given <tt>PreparedStatement</tt>, advance to the first result and return SQL <tt>ResultSet</tt>.
SQLException
HibernateException
protected final ResultSet getResultSet(CallableStatement st, RowSelection selection, LimitHandler limitHandler, boolean autodiscovertypes, SharedSessionContractImplementor session) throws SQLException, HibernateException
Execute given <tt>CallableStatement</tt>, advance to the first result and return SQL <tt>ResultSet</tt>.
SQLException
HibernateException
protected void autoDiscoverTypes(ResultSet rs)
protected final List loadEntity(SharedSessionContractImplementor session, Object id, Type identifierType, Object optionalObject, String optionalEntityName, Serializable optionalIdentifier, EntityPersister persister, LockOptions lockOptions) throws HibernateException
Called by subclasses that load entities
HibernateException
protected final List loadEntity(SharedSessionContractImplementor session, Object key, Object index, Type keyType, Type indexType, EntityPersister persister) throws HibernateException
Called by subclasses that load entities
persister
- only needed for loggingHibernateException
public final List loadEntityBatch(SharedSessionContractImplementor session, Serializable[] ids, Type idType, Object optionalObject, String optionalEntityName, Serializable optionalId, EntityPersister persister, LockOptions lockOptions) throws HibernateException
Called by wrappers that batch load entities
HibernateException
public final void loadCollection(SharedSessionContractImplementor session, Serializable id, Type type) throws HibernateException
Called by subclasses that initialize collections
HibernateException
public final void loadCollectionBatch(SharedSessionContractImplementor session, Serializable[] ids, Type type) throws HibernateException
Called by wrappers that batch initialize collections
HibernateException
protected final void loadCollectionSubselect(SharedSessionContractImplementor session, Serializable[] ids, Object[] parameterValues, Type[] parameterTypes, Map<String,TypedValue> namedParameters, Type type) throws HibernateException
Called by subclasses that batch initialize collections
HibernateException
protected List list(SharedSessionContractImplementor session, QueryParameters queryParameters, Set<Serializable> querySpaces, Type[] resultTypes) throws HibernateException
Return the query results, using the query cache, called by subclasses that implement cacheable queries
HibernateException
protected void putResultInQueryCache(SharedSessionContractImplementor session, QueryParameters queryParameters, Type[] resultTypes, QueryResultsCache queryCache, QueryKey key, List result)
protected List doList(SharedSessionContractImplementor session, QueryParameters queryParameters) throws HibernateException
Actually execute a query, ignoring the query cache
HibernateException
protected void checkScrollability() throws HibernateException
Check whether the current loader can support returning ScrollableResults.
HibernateException
protected boolean needsFetchingScroll()
Does the result set to be scrolled contain collection fetches?
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 <tt>ScrollableResults</tt>
queryParameters
- The parameters with which the query should be executed.returnTypes
- The expected return types of the queryholderInstantiator
- 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.HibernateException
- Indicates an error executing the query, or constructing
the ScrollableResults.protected void postInstantiate()
Calculate and cache select-clause suffixes. Must be called by subclasses after instantiation.
protected abstract EntityAliases[] getEntityAliases()
Get the result set descriptor
protected abstract CollectionAliases[] getCollectionAliases()
protected String getQueryIdentifier()
Identifies the query for statistics reporting, if null, no statistics will be reported
public final SessionFactoryImplementor getFactory()
protected String processDistinctKeyword(String sql, QueryParameters parameters)
Remove distinct keyword from SQL statement if the query should not pass it through.
sql
- SQL stringparameters
- SQL parametersCopyright © 2001-2018 Red Hat, Inc. All Rights Reserved.