⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loader.java

📁 hibernate-3.1.3-all-src.zip 面向对象的访问数据库工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			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(final PreparedStatement ps,
			  final Map namedParams,
			  final int start,
			  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] + start ) + "]" 
							);
					}
					typedval.getType().nullSafeSet( ps, typedval.getValue(), locs[i] + start, session );
				}
				result += locs.length;
			}
			return result;
		}
		else {
			return 0;
		}
	}
	
	public int[] getNamedParameterLocs(String name) {
		throw new AssertionFailure("no named parameters");
	}

	/**
	 * 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 ) );
	}

	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, 
				queryParameters, 
				querySpaces, 
				resultTypes, 
				queryCache, 
				key 
			);

		if ( result == null ) {
			result = doList( session, queryParameters );

			putResultInQueryCache( 
					session, 
					queryParameters, 
					resultTypes,
					queryCache, 
					key, 
					result 
				);
		}

		return getResultList( result );
	}

	private List getResultFromQueryCache(
			final SessionImplementor session, 
			final QueryParameters queryParameters, 
			final Set querySpaces, 
			final Type[] resultTypes,
			final QueryCache queryCache, 
			final QueryKey key) {
		List result = null;
		
		if ( session.getCacheMode().isGetEnabled() ) {
			result = queryCache.get( 
					key, 
					resultTypes, 
					queryParameters.isNaturalKeyLookup(), 
					querySpaces, 
					session 
				);

			if ( factory.getStatistics().isStatisticsEnabled() ) {
				if (result==null) {
					factory.getStatisticsImplementor()
							.queryCacheMiss( getQueryIdentifier(), queryCache.getRegionName() );
				}
				else {
					factory.getStatisticsImplementor()
							.queryCacheHit( getQueryIdentifier(), queryCache.getRegionName() );
				}
			}
			
		}
		
		return result;
	}

	private void putResultInQueryCache(
			final SessionImplementor session, 
			final QueryParameters queryParameters, 
			final Type[] resultTypes,
			final QueryCache queryCache, 
			final QueryKey key, 
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -