📄 loader.java
字号:
types[i].nullSafeSet( statement, values[i], startIndex + span, session ); span += types[i].getColumnSpan( getFactory() ); } return span; } /** * 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... * * @param statement The JDBC prepared statement * @param namedParams A map of parameter names to values * @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. * @throws org.hibernate.HibernateException Indicates problems delegating binding to the types. */ protected int bindNamedParameters( final PreparedStatement statement, final Map namedParams, final int startIndex, final SessionImplementor session) throws SQLException, HibernateException { if ( namedParams != null ) { // assumes that types are all of span 1 Iterator iter = namedParams.entrySet().iterator(); int result = 0; while ( iter.hasNext() ) { Map.Entry e = ( Map.Entry ) iter.next(); String name = ( String ) e.getKey(); TypedValue typedval = ( TypedValue ) e.getValue(); int[] locs = getNamedParameterLocs( name ); for ( int i = 0; i < locs.length; i++ ) { if ( log.isDebugEnabled() ) { log.debug( "bindNamedParameters() " + typedval.getValue() + " -> " + name + " [" + ( locs[i] + startIndex ) + "]" ); } typedval.getType().nullSafeSet( statement, typedval.getValue(), locs[i] + startIndex, session ); } result += locs.length; } return result; } else { return 0; } } public int[] getNamedParameterLocs(String name) { throw new AssertionFailure("no named parameters"); } /** * Fetch a <tt>PreparedStatement</tt>, call <tt>setMaxRows</tt> and then execute it, * advance to the first result and return an SQL <tt>ResultSet</tt> */ protected final ResultSet getResultSet( final PreparedStatement st, final boolean autodiscovertypes, final boolean callable, final RowSelection selection, final SessionImplementor session) throws SQLException, HibernateException { ResultSet rs = null; try { Dialect dialect = getFactory().getDialect(); if (callable) { rs = session.getBatcher().getResultSet( (CallableStatement) st, dialect ); } else { rs = session.getBatcher().getResultSet( st ); } rs = wrapResultSetIfEnabled( rs , session ); if ( !dialect.supportsLimitOffset() || !useLimit( selection, dialect ) ) { advance( rs, selection ); } if ( autodiscovertypes ) { autoDiscoverTypes( rs ); } return rs; } catch ( SQLException sqle ) { session.getBatcher().closeQueryStatement( st, rs ); throw sqle; } } protected void autoDiscoverTypes(ResultSet rs) { throw new AssertionFailure("Auto discover types not supported in this loader"); } 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; } /** * 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 subclasses that load entities * @param persister only needed for logging */ protected final List loadEntity( final SessionImplementor session, final Object key, final Object index, final Type keyType, final Type indexType, final EntityPersister persister) throws HibernateException { if ( log.isDebugEnabled() ) { log.debug( "loading collection element by index" ); } List result; try { result = doQueryAndInitializeNonLazyCollections( session, new QueryParameters( new Type[] { keyType, indexType }, new Object[] { key, index } ), false ); } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( factory.getSQLExceptionConverter(), sqle, "could not collection element by index", 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( getCollectionPersisters()[0], 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( getCollectionPersisters()[0], 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( getCollectionPersisters()[0], 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( getCollectionPersisters()[0], 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( getCollectionPersisters()[0], 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 ) { return listUsingQueryCache( session, queryParameters, querySpaces, resultTypes ); } else { return listIgnoreQueryCache( session, queryParameters ); } } private List listIgnoreQueryCache(SessionImplementor session, QueryParameters queryParameters) { return getResultList( doList( session, queryParameters ), queryParameters.getResultTransformer() ); } private List listUsingQueryCache( final SessionImplementor session, final QueryParameters queryParameters, final Set querySpaces, final Type[] resultTypes) { QueryCache queryCache = factory.getQueryCache( queryParameters.getCacheRegion() ); Set filterKeys = FilterKey.createFilterKeys( session.getEnabledFilters(), session.getEntityMode() ); QueryKey key = new QueryKey( getSQLString(), queryParameters, filterKeys, session.getEntityMode() ); List result = getResultFromQueryCache( session, queryPa
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -