Top Description Fields Constructors Methods
org.hibernate.loader

public abstract Class Loader

extends Object
Class Inheritance
Known Direct Subclasses
org.hibernate.loader.custom.CustomLoader, org.hibernate.loader.BasicLoader
Imports
java.io.Serializable, java.sql.CallableStatement, .PreparedStatement, .ResultSet, .SQLException, java.util.ArrayList, .Arrays, .HashMap, .HashSet, .Iterator, .List, .Map, .Set, org.slf4j.Logger, .LoggerFactory, org.hibernate.AssertionFailure, .HibernateException, .LockMode, .QueryException, .ScrollMode, .ScrollableResults, .StaleObjectStateException, .WrongClassException, .LockOptions, org.hibernate.cache.FilterKey, .QueryCache, .QueryKey, org.hibernate.collection.PersistentCollection, org.hibernate.dialect.Dialect, org.hibernate.engine.EntityKey, .EntityUniqueKey, .PersistenceContext, .QueryParameters, .RowSelection, .SessionFactoryImplementor, .SessionImplementor, .SubselectFetch, .TwoPhaseLoad, .TypedValue, org.hibernate.engine.jdbc.ColumnNameCache, org.hibernate.event.EventSource, .PostLoadEvent, .PreLoadEvent, org.hibernate.exception.JDBCExceptionHelper, org.hibernate.hql.HolderInstantiator, org.hibernate.impl.FetchingScrollableResultsImpl, .ScrollableResultsImpl, org.hibernate.persister.collection.CollectionPersister, org.hibernate.persister.entity.EntityPersister, .Loadable, .UniqueKeyLoadable, org.hibernate.pretty.MessageHelper, org.hibernate.proxy.HibernateProxy, org.hibernate.transform.ResultTransformer, org.hibernate.type.AssociationType, .EntityType, .Type, .VersionType, org.hibernate.util.StringHelper

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.
Author
Gavin King
See Also
org.hibernate.persister.entity.Loadable

Field Summary

Modifier and TypeField and Description
private ColumnNameCache
private final SessionFactoryImplementor
private static final Logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
advance(final ResultSet rs, final RowSelection selection)

Advance the cursor to the first required row of the ResultSet

protected String
applyLocks(String sql, LockOptions lockOptions, Dialect dialect)

Append FOR UPDATE OF clause, if necessary.

protected void
applyPostLoadLocks(Object[] row, LockMode[] lockModesArray, SessionImplementor session)

protected boolean

Returns:

true, if getResultColumnOrRow() transforms the results; false, otherwise
areResultSetRowsTransformedImmediately
(ResultTransformer
the specified transformer
transformer
)

Are rows transformed immediately after being read from the ResultSet?

protected void
private int

Returns:

The number of parameter values bound.
bindLimitParameters
(final PreparedStatement
The statement to which to bind limit param values.
statement
,
final int
The bind position from which to start binding
index
,
final RowSelection
The selection object containing the limit information.
selection
)

Bind parameter values needed by the dialect-specific LIMIT clause.

protected int

Returns:

The number of JDBC bind positions actually bound during this method execution.
bindNamedParameters
(final PreparedStatement
The JDBC prepared statement
statement
,
final Map<K, V>
A map of parameter names to values
namedParams
,
final int
The position from which to start binding parameter values.
startIndex
,
final SessionImplementor
The originating session.
session
)

Bind named parameters to the JDBC prepared statement.

protected int

Returns:

The number of JDBC bind positions actually bound during this method execution.
bindParameterValues
(PreparedStatement
The JDBC prepared statement
statement
,
QueryParameters
The encapsulation of the parameter values to be bound.
queryParameters
,
int
The position from which to start binding parameter values.
startIndex
,
SessionImplementor
The originating session.
session
)

Bind all parameter values into the prepared statement in preparation for execution.

protected int

Returns:

The number of JDBC bind positions actually bound during this method execution.
bindPositionalParameters
(final PreparedStatement
The JDBC prepared statement
statement
,
final QueryParameters
The encapsulation of the parameter values to be bound.
queryParameters
,
final int
The position from which to start binding parameter values.
startIndex
,
final SessionImplementor
The originating session.
session
)

Bind positional parameter values to the JDBC prepared statement.

private Map<K, V>
protected void
checkScrollability()

Check whether the current loader can support returning ScrollableResults.

private void
checkVersion(final int i, final Loadable persister, final Serializable id, final Object entity, final ResultSet rs, final SessionImplementor session)

Check the version of the object in the ResultSet against the object version in the session cache, throwing an exception if the version numbers are different

private void
createSubselects(List<E> keys, QueryParameters queryParameters, SessionImplementor session)

private Serializable
determineResultId(SessionImplementor session, Serializable optionalId, Type idType, Serializable resolvedId)

protected List<E>
doList(final SessionImplementor session, final QueryParameters queryParameters)

Actually execute a query, ignoring the query cache

private List<E>
doQuery(final SessionImplementor session, final QueryParameters queryParameters, final boolean returnProxies)

private List<E>
doQueryAndInitializeNonLazyCollections(final SessionImplementor session, final QueryParameters queryParameters, final boolean returnProxies)

Execute an SQL query and attempt to instantiate instances of the class mapped by the given persister from each row of the ResultSet.

private void
endCollectionLoad(final Object resultSetId, final SessionImplementor session, final CollectionPersister collectionPersister)

protected void
extractKeysFromResultSet(Loadable[] persisters, QueryParameters queryParameters, ResultSet resultSet, SessionImplementor session, EntityKey[] keys, LockMode[] lockModes, List<E> 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[]
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[][]
protected abstract EntityAliases[]
getEntityAliases()

Get the result set descriptor

protected boolean[]

Returns:

Eager property fetching indicators.
getEntityEagerPropertyFetches
()

An array indicating whether the entities have eager property fetching enabled.

private EntityPersister
protected abstract Loadable[]

Returns:

The entity persisters.
getEntityPersisters
()

An array of persisters of entity classes contained in each row of results; implemented by all subclasses

public final SessionFactoryImplementor
private static int
private String
getInstanceClass(final ResultSet rs, final int i, final Loadable persister, final Serializable id, final SessionImplementor session)

Determine the concrete class of an instance in the ResultSet

private EntityKey
getKeyFromResultSet(final int i, final Loadable persister, final Serializable id, final ResultSet rs, final SessionImplementor session)

Read a row of Keys from the ResultSet into the given array.

protected abstract LockMode[]
getLockModes(LockOptions
a collection of lock options specified dynamically via the Query interface
lockOptions
)

What lock options does this load entities with?

private static int

Returns:

The appropriate value to bind into the limit clause.
getMaxOrLimit
(final RowSelection
The selection criteria
selection
,
final Dialect
The dialect
dialect
)

Some dialect-specific LIMIT clauses require the maximum last row number (aka, first_row_number + total_row_count), while others require the maximum returned row count (the total maximum number of rows to return).

public int[]
private static EntityKey
protected EntityType[]

Returns:

The types for the owners.
getOwnerAssociationTypes
()

An array of the owner types corresponding to the getOwners() returns.

protected int[]

Returns:

The owner indicators (see discussion above).
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, SessionImplementor session)

Get the actual object that is returned in the user-visible result list.

private List<E>
getResultFromQueryCache(final SessionImplementor session, final QueryParameters queryParameters, final Set<E> querySpaces, final Type[] resultTypes, final QueryCache queryCache, final QueryKey key)

protected List<E>
getResultList(List<E> results, ResultTransformer resultTransformer)

protected final ResultSet
getResultSet(final PreparedStatement st, final boolean autodiscovertypes, final boolean callable, final RowSelection selection, final SessionImplementor session)

Fetch a PreparedStatement, call setMaxRows and then execute it, advance to the first result and return an SQL ResultSet

private Object[]
getRow(final ResultSet rs, final Loadable[] persisters, final EntityKey[] keys, final Object optionalObject, final EntityKey optionalObjectKey, final LockMode[] lockModes, final List<E> hydratedObjects, final SessionImplementor session)

Resolve any IDs for currently loaded objects, duplications within the ResultSet, etc. Instantiate empty objects to be initialized from the ResultSet.

private Object
getRowFromResultSet(final ResultSet resultSet, final SessionImplementor session, final QueryParameters queryParameters, final LockMode[] lockModesArray, final EntityKey optionalObjectKey, final List<E> hydratedObjects, final EntityKey[] keys, boolean returnProxies)

protected abstract String

Returns:

The sql command this loader should use to get its ResultSet.
getSQLString
()

The SQL query string to be called; implemented by all subclasses

private void
handleEmptyCollections(final Serializable[] keys, final Object resultSetId, final SessionImplementor 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).

private static boolean
protected boolean
private void
initializeEntitiesAndCollections(final List<E> hydratedObjects, final Object resultSetId, final SessionImplementor session, final boolean readOnly)

private void
instanceAlreadyLoaded(final ResultSet rs, final int i, final Loadable persister, final EntityKey key, final Object object, final LockMode lockMode, final SessionImplementor session)

The entity instance is already in the session cache

private Object
instanceNotYetLoaded(final ResultSet rs, final int i, final Loadable persister, final String rowIdAlias, final EntityKey key, final LockMode lockMode, final EntityKey optionalObjectKey, final Object optionalObject, final List<E> hydratedObjects, final SessionImplementor session)

The entity instance is not in the session cache

private int
interpretFirstRow(int zeroBasedFirstResult)

private boolean
protected boolean
isSingleRowLoader()

Return false is this loader is a batch entity loader

protected boolean
protected List<E>
list(final SessionImplementor session, final QueryParameters queryParameters, final Set<E> querySpaces, final Type[] resultTypes)

Return the query results, using the query cache, called by subclasses that implement cacheable queries

private List<E>
private List<E>
listUsingQueryCache(final SessionImplementor session, final QueryParameters queryParameters, final Set<E> querySpaces, final Type[] resultTypes)

public final void
loadCollection(final SessionImplementor session, final Serializable id, final Type type)

Called by subclasses that initialize collections

public final void
loadCollectionBatch(final SessionImplementor session, final Serializable[] ids, final Type type)

Called by wrappers that batch initialize collections

protected final void
loadCollectionSubselect(final SessionImplementor session, final Serializable[] ids, final Object[] parameterValues, final Type[] parameterTypes, final Map<K, V> namedParameters, final Type type)

Called by subclasses that batch initialize collections

protected final List<E>
loadEntity(final SessionImplementor session, final Object id, final Type identifierType, final Object optionalObject, final String optionalEntityName, final Serializable optionalIdentifier, final EntityPersister
only needed for logging
persister
,
LockOptions lockOptions)

Called by subclasses that load entities

protected final List<E>
loadEntity(final SessionImplementor session, final Object key, final Object index, final Type keyType, final Type indexType, final EntityPersister
only needed for logging
persister
)

Called by subclasses that load entities

public final List<E>
loadEntityBatch(final SessionImplementor session, final Serializable[] ids, final Type idType, final Object optionalObject, final String optionalEntityName, final Serializable optionalId, final EntityPersister
only needed for logging
persister
,
LockOptions lockOptions)

Called by wrappers that batch load entities

private void
loadFromResultSet(final ResultSet rs, final int i, final Object object, final String instanceEntityName, final EntityKey key, final String rowIdAlias, final LockMode lockMode, final Loadable rootPersister, final SessionImplementor session)

Hydrate the state an object from the SQL ResultSet, into an array or "hydrated" values (do not resolve associations yet), and pass the hydrates state to the session.

public Object

Returns:

The loaded "row".
loadSequentialRowsForward
(final ResultSet
The result set from which to do the load.
resultSet
,
final SessionImplementor
The session from which the request originated.
session
,
final QueryParameters
The query parameters specified by the user.
queryParameters
,
final boolean
Should proxies be generated
returnProxies
)

Loads a single logical row from the result set moving forward.

public Object

Returns:

The loaded "row".
loadSequentialRowsReverse
(final ResultSet
The result set from which to do the load.
resultSet
,
final SessionImplementor
The session from which the request originated.
session
,
final QueryParameters
The query parameters specified by the user.
queryParameters
,
final boolean
Should proxies be generated
returnProxies
,
final boolean isLogicallyAfterLast)

Loads a single logical row from the result set moving forward.

public Object

Returns:

The loaded "row".
loadSingleRow
(final ResultSet
The result set from which to do the load.
resultSet
,
final SessionImplementor
The session from which the request originated.
session
,
final QueryParameters
The query parameters specified by the user.
queryParameters
,
final boolean
Should proxies be generated
returnProxies
)

Loads a single row from the result set.

private void
logCachedResultDetails(ResultTransformer resultTransformer, Type[] returnTypes, List<E> result)

protected boolean

Returns:

True if it does, and thus needs the special fetching scroll functionality; false otherwise.
needsFetchingScroll
()

Does the result set to be scrolled contain collection fetches?

protected void
postInstantiate()

Calculate and cache select-clause suffixes.

protected final PreparedStatement
prepareQueryStatement(final QueryParameters queryParameters, final boolean scroll, final SessionImplementor session)

Obtain a PreparedStatement with all parameters pre-bound.

private String
protected String
preprocessSQL(String sql, QueryParameters parameters, Dialect dialect)

Modify the SQL, adding lock hints and comments, if necessary

private void
putResultInQueryCache(final SessionImplementor session, final QueryParameters queryParameters, final Type[] resultTypes, final QueryCache queryCache, final QueryKey key, final List<E> result)

private void
readCollectionElement(final Object optionalOwner, final Serializable optionalKey, final CollectionPersister persister, final CollectionAliases descriptor, final ResultSet rs, final SessionImplementor session)

Read one collection element from the current row of the JDBC result set

private void
readCollectionElements(Object[] row, ResultSet resultSet, SessionImplementor session)

Read any collection elements contained in a single row of the result set

private void
registerNonExists(final EntityKey[] keys, final Loadable[] persisters, final SessionImplementor 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.

protected ResultTransformer

Returns:

the actual result transformer
resolveResultTransformer
(ResultTransformer
the specified result transformer
resultTransformer
)

Determine the actual ResultTransformer that will be used to transform query results.

private ColumnNameCache
protected ScrollableResults

Returns:

The ScrollableResults instance.
scroll
(final QueryParameters
The parameters with which the query should be executed.
queryParameters
,
final Type[]
The expected return types of the query
returnTypes
,
final HolderInstantiator
If the return values are expected to be wrapped in a holder, this is the thing that knows how to wrap them.
holderInstantiator
,
final SessionImplementor
The session from which the scroll request originated.
session
)

Return the query results, as an instance of ScrollableResults

private Object
sequentialLoad(final ResultSet resultSet, final SessionImplementor session, final QueryParameters queryParameters, final boolean returnProxies, final EntityKey keyToRead)

private void
setMaxRows(final PreparedStatement st, final RowSelection selection)

Use JDBC API to limit the number of rows returned by the SQL query if necessary

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
private static Set<E>[]
transpose(List<E> keys)

protected boolean
upgradeLocks()

Does this query return objects that might be already cached by the session, whose lock mode may need upgrading

private static boolean
useLimit(final RowSelection selection, final Dialect dialect)

Should we pre-process the SQL string, adding a dialect-specific LIMIT clause.

private synchronized ResultSet
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait