📄 loader.java
字号:
private synchronized ResultSet wrapResultSetIfEnabled(final ResultSet rs, final SessionImplementor session) { // synchronized to avoid multi-thread access issues; defined as method synch to avoid // potential deadlock issues due to nature of code. if ( session.getFactory().getSettings().isWrapResultSetsEnabled() ) { try { log.debug("Wrapping result set [" + rs + "]"); return new ResultSetWrapper( rs, retreiveColumnNameToIndexCache( rs ) ); } catch(SQLException e) { log.info("Error wrapping result set", e); return rs; } } else { return rs; } } private ColumnNameCache retreiveColumnNameToIndexCache(ResultSet rs) throws SQLException { if ( columnNameCache == null ) { log.trace("Building columnName->columnIndex cache"); columnNameCache = new ColumnNameCache( rs.getMetaData().getColumnCount() ); } return columnNameCache; } /** * Bind named parameters to the <tt>PreparedStatement</tt>. This has an empty * implementation on this superclass and should be implemented by subclasses * (queries) which allow named parameters. */ protected int bindNamedParameters(PreparedStatement st, Map namedParams, int start, SessionImplementor session) throws SQLException, HibernateException { return 0; } /** * Called by subclasses that load entities * @param persister only needed for logging */ protected final List loadEntity(final SessionImplementor session, final Object id, final Type identifierType, final Object optionalObject, final String optionalEntityName, final Serializable optionalIdentifier, final EntityPersister persister) throws HibernateException { if ( log.isDebugEnabled() ) { log.debug( "loading entity: " + MessageHelper.infoString( persister, id, identifierType, getFactory() ) ); } List result; try { result = doQueryAndInitializeNonLazyCollections( session, new QueryParameters( new Type[]{identifierType}, new Object[]{id}, optionalObject, optionalEntityName, optionalIdentifier ), false ); } catch ( SQLException sqle ) { final Loadable[] persisters = getEntityPersisters(); throw JDBCExceptionHelper.convert( factory.getSQLExceptionConverter(), sqle, "could not load an entity: " + MessageHelper.infoString( persisters[persisters.length-1], id, identifierType, getFactory() ), getSQLString() ); } log.debug("done entity load"); return result; } /** * Called by wrappers that batch load entities * @param persister only needed for logging */ public final List loadEntityBatch(final SessionImplementor session, final Serializable[] ids, final Type idType, final Object optionalObject, final String optionalEntityName, final Serializable optionalId, final EntityPersister persister) throws HibernateException { if ( log.isDebugEnabled() ) { log.debug( "batch loading entity: " + MessageHelper.infoString(persister, ids, getFactory() ) ); } Type[] types = new Type[ids.length]; Arrays.fill( types, idType ); List result; try { result = doQueryAndInitializeNonLazyCollections( session, new QueryParameters( types, ids, optionalObject, optionalEntityName, optionalId ), false ); } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( factory.getSQLExceptionConverter(), sqle, "could not load an entity batch: " + MessageHelper.infoString( getEntityPersisters()[0], ids, getFactory() ), getSQLString() ); } log.debug("done entity batch load"); return result; } /** * Called by subclasses that initialize collections */ public final void loadCollection(final SessionImplementor session, final Serializable id, final Type type) throws HibernateException { if ( log.isDebugEnabled() ) { log.debug( "loading collection: "+ MessageHelper.collectionInfoString( getCollectionPersister(), id, getFactory() ) ); } Serializable[] ids = new Serializable[]{id}; try { doQueryAndInitializeNonLazyCollections( session, new QueryParameters( new Type[]{type}, ids, ids ), true ); } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( factory.getSQLExceptionConverter(), sqle, "could not initialize a collection: " + MessageHelper.collectionInfoString( getCollectionPersister(), id, getFactory() ), getSQLString() ); } log.debug("done loading collection"); } /** * Called by wrappers that batch initialize collections */ public final void loadCollectionBatch(final SessionImplementor session, final Serializable[] ids, final Type type) throws HibernateException { if ( log.isDebugEnabled() ) { log.debug( "batch loading collection: "+ MessageHelper.collectionInfoString( getCollectionPersister(), ids, getFactory() ) ); } Type[] idTypes = new Type[ids.length]; Arrays.fill( idTypes, type ); try { doQueryAndInitializeNonLazyCollections( session, new QueryParameters( idTypes, ids, ids ), true ); } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( factory.getSQLExceptionConverter(), sqle, "could not initialize a collection batch: " + MessageHelper.collectionInfoString( getCollectionPersister(), ids, getFactory() ), getSQLString() ); } log.debug("done batch load"); } /** * Called by subclasses that batch initialize collections */ protected final void loadCollectionSubselect(final SessionImplementor session, final Serializable[] ids, final Object[] parameterValues, final Type[] parameterTypes, final Map namedParameters, final Type type) throws HibernateException { Type[] idTypes = new Type[ids.length]; Arrays.fill( idTypes, type ); try { doQueryAndInitializeNonLazyCollections( session, new QueryParameters( parameterTypes, parameterValues, namedParameters, ids ), true ); } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( factory.getSQLExceptionConverter(), sqle, "could not load collection by subselect: " + MessageHelper.collectionInfoString( getCollectionPersister(), ids, getFactory() ), getSQLString() ); } } /** * Return the query results, using the query cache, called * by subclasses that implement cacheable queries */ protected List list(final SessionImplementor session, final QueryParameters queryParameters, final Set querySpaces, final Type[] resultTypes) throws HibernateException { final boolean cacheable = factory.getSettings().isQueryCacheEnabled() && queryParameters.isCacheable(); if ( cacheable ) { final boolean queryStatisticsEnabled = getQueryIdentifier() != null && factory.getStatistics().isStatisticsEnabled(); QueryCache queryCache = factory.getQueryCache( queryParameters.getCacheRegion() ); QueryKey key = new QueryKey( getSQLString(), queryParameters, session.getEntityMode() ); List result = null; if ( /*!queryParameters.isForceCacheRefresh() &&*/ session.getCacheMode().isGetEnabled() ) { result = queryCache.get( key, resultTypes, querySpaces, session ); if (queryStatisticsEnabled) { if (result==null) { factory.getStatisticsImplementor().queryCacheMiss( getQueryIdentifier(), queryCache.getRegionName() ); } else { factory.getStatisticsImplementor().queryCacheHit( getQueryIdentifier(), queryCache.getRegionName() ); } } } if ( result == null ) { result = doList( session, queryParameters ); if ( cacheable && session.getCacheMode().isPutEnabled() ) { queryCache.put( key, resultTypes, result, session ); if ( queryStatisticsEnabled ) { factory.getStatisticsImplementor().queryCachePut( getQueryIdentifier(), queryCache.getRegionName() ); } } } return getResultList( result ); } else { return getResultList( doList( session, queryParameters ) ); } } /** * Actually execute a query, ignoring the query cache */ protected List doList(final SessionImplementor session, final QueryParameters queryParameters) throws HibernateException { final boolean stats = getQueryIdentifier() != null && getFactory().getStatistics().isStatisticsEnabled(); long startTime = 0; if ( stats ) startTime = System.currentTimeMillis(); List result; try { result = doQueryAndInitializeNonLazyCollections( session, queryParameters, true ); } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( factory.getSQLExceptionConverter(), sqle, "could not execute query", getSQLString() ); } if ( stats ) { getFactory().getStatisticsImplementor().queryExecuted( getQueryIdentifier(), result.size(), System.currentTimeMillis() - startTime ); } return result; } /** * Return the query results, as an instance of <tt>ScrollableResults</tt> */ protected ScrollableResults scroll(final QueryParameters queryParameters, final Type[] returnTypes, final Class holderClass, final SessionImplementor session) throws HibernateException { if ( getCollectionPersister() != null ) { throw new HibernateException( "Cannot scroll queries which initialize collections" ); } final boolean stats = getQueryIdentifier() != null && getFactory().getStatistics().isStatisticsEnabled(); long startTime = 0; if ( stats ) startTime = System.currentTimeMillis(); try { PreparedStatement st = prepareQueryStatement( queryParameters, true, session ); ResultSet rs = getResultSet( st, queryParameters.getRowSelection(), session ); if ( stats ) { getFactory().getStatisticsImplementor().queryExecuted( getQueryIdentifier(), 0, System.currentTimeMillis() - startTime ); } ScrollableResults result = new ScrollableResultsImpl( rs, st, session, this, queryParameters, returnTypes, holderClass ); return result; } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( factory.getSQLExceptionConverter(), sqle, "could not execute query using scroll", getSQLString() ); } } /** * Calculate and cache select-clause suffixes. Must be * called by subclasses after instantiation. */ protected void postInstantiate() {} /** * Get the result set descriptor */ protected abstract EntityAliases[] getEntityAliases(); /** * Identifies the query for statistics reporting, if null, * no statistics will be reported */ protected String getQueryIdentifier() { return null; } public final SessionFactoryImplementor getFactory() { return factory; } public String toString() { return getClass().getName() + '(' + getSQLString() + ')'; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -