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

📄 sessionimpl.java

📁 hibernate-3.1.3-all-src.zip 面向对象的访问数据库工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

		autoFlushIfRequired(spaces);

		List results = Collections.EMPTY_LIST;
		dontFlushFromFind++;
		boolean success = false;
		try {
			for( int i=0; i<size; i++ ) {
				final List currentResults = loaders[i].list(this);
				currentResults.addAll(results);
				results = currentResults;
			}
			success = true;
		}
		finally {
			dontFlushFromFind--;
			afterOperation(success);
		}

		return results;
	}

	private OuterJoinLoadable getOuterJoinLoadable(String entityName) throws MappingException {
		EntityPersister persister = factory.getEntityPersister(entityName);
		if ( !(persister instanceof OuterJoinLoadable) ) {
			throw new MappingException( "class persister is not OuterJoinLoadable: " + entityName );
		}
		return ( OuterJoinLoadable ) persister;
	}

	public boolean contains(Object object) {
		errorIfClosed();
		checkTransactionSynchStatus();
		if ( object instanceof HibernateProxy ) {
			//do not use proxiesByKey, since not all
			//proxies that point to this session's
			//instances are in that collection!
			LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
			if ( li.isUninitialized() ) {
				//if it is an uninitialized proxy, pointing
				//with this session, then when it is accessed,
				//the underlying instance will be "contained"
				return li.getSession()==this;
			}
			else {
				//if it is initialized, see if the underlying
				//instance is contained, since we need to 
				//account for the fact that it might have been
				//evicted
				object = li.getImplementation();
			}
		}
		return persistenceContext.isEntryFor(object);
	}
	
	public Query createQuery(String queryString) {
		errorIfClosed();
		checkTransactionSynchStatus();
		return super.createQuery(queryString);
	}
	
	public SQLQuery createSQLQuery(String sql) {
		errorIfClosed();
		checkTransactionSynchStatus();
		return super.createSQLQuery(sql);
	}

	public Query createSQLQuery(String sql, String returnAlias, Class returnClass) {
		errorIfClosed();
		checkTransactionSynchStatus();
		return new SQLQueryImpl(
				sql,
		        new String[] { returnAlias },
		        new Class[] { returnClass },
		        this,
		        factory.getQueryPlanCache().getSQLParameterMetadata( sql )
		);
	}

	public Query createSQLQuery(String sql, String returnAliases[], Class returnClasses[]) {
		errorIfClosed();
		checkTransactionSynchStatus();
		return new SQLQueryImpl(
				sql,
		        returnAliases,
		        returnClasses,
		        this,
		        factory.getQueryPlanCache().getSQLParameterMetadata( sql )
		);
	}

	public ScrollableResults scrollCustomQuery(CustomQuery customQuery, QueryParameters queryParameters) 
	throws HibernateException {
		errorIfClosed();
		checkTransactionSynchStatus();

		if ( log.isTraceEnabled() ) {
			log.trace( "scroll SQL query: " + customQuery.getSQL() );
		}

		CustomLoader loader = new CustomLoader( customQuery, getFactory() );

		autoFlushIfRequired( loader.getQuerySpaces() );

		dontFlushFromFind++; //stops flush being called multiple times if this method is recursively called
		try {
			return loader.scroll(queryParameters, this);
		}
		finally {
			dontFlushFromFind--;
		}
	}

	// basically just an adapted copy of find(CriteriaImpl)
	public List listCustomQuery(CustomQuery customQuery, QueryParameters queryParameters) 
	throws HibernateException {
		errorIfClosed();
		checkTransactionSynchStatus();

		if ( log.isTraceEnabled() ) {
			log.trace( "SQL query: " + customQuery.getSQL() );
		}
		
		CustomLoader loader = new CustomLoader( customQuery, getFactory() );

		autoFlushIfRequired( loader.getQuerySpaces() );

		dontFlushFromFind++;
		boolean success = false;
		try {
			List results = loader.list(this, queryParameters);
			success = true;
			return results;
		}
		finally {
			dontFlushFromFind--;
			afterOperation(success);
		}
	}

	public SessionFactory getSessionFactory() {
		checkTransactionSynchStatus();
		return factory;
	}
	
	public void initializeCollection(PersistentCollection collection, boolean writing) 
	throws HibernateException {
		errorIfClosed();
		checkTransactionSynchStatus();
		InitializeCollectionEventListener[] listener = listeners.getInitializeCollectionEventListeners();
		for ( int i = 0; i < listener.length; i++ ) {
			listener[i].onInitializeCollection( new InitializeCollectionEvent(collection, this) );
		}
	}

	public String bestGuessEntityName(Object object) {
		if (object instanceof HibernateProxy) {
			object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation();
		}
		EntityEntry entry = persistenceContext.getEntry(object);
		if (entry==null) {
			return guessEntityName(object);
		}
		else {
			return entry.getPersister().getEntityName();
		}
	}
	
	public String getEntityName(Object object) {
		errorIfClosed();
		checkTransactionSynchStatus();
		if (object instanceof HibernateProxy) {
			if ( !persistenceContext.containsProxy( object ) ) {
				throw new TransientObjectException("proxy was not associated with the session");
			}
			object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation();
		}

		EntityEntry entry = persistenceContext.getEntry(object);
		if ( entry == null ) {
			throwTransientObjectException( object );
		}
		return entry.getPersister().getEntityName();
	}

	private void throwTransientObjectException(Object object) throws HibernateException {
		throw new TransientObjectException(
				"object references an unsaved transient instance - save the transient instance before flushing: " +
				guessEntityName(object)
			);
	}

	public String guessEntityName(Object object) throws HibernateException {
		errorIfClosed();
		String entity = interceptor.getEntityName( object );
		if ( entity == null ) {
			if ( object instanceof Map ) {
				entity = (String) ( (Map) object ).get( DynamicMapInstantiator.KEY );
				if ( entity == null ) {
					throw new HibernateException( "could not determine type of dynamic entity" );
				}
			}
			else if ( object instanceof Element ) {
				// TODO : really need to keep a map of nodeName -> entityName, but that would mean nodeName being distinct
				entity = ( (Element) object ).getName();
			}
			else {
				entity = object.getClass().getName();
			}
		}
		return entity;
	}

	public void cancelQuery() throws HibernateException {
		errorIfClosed();
		getBatcher().cancelLastQuery();
	}

	public Interceptor getInterceptor() {
		checkTransactionSynchStatus();
		return interceptor;
	}

	public int getDontFlushFromFind() {
		return dontFlushFromFind;
	}

	public String toString() {
		StringBuffer buf = new StringBuffer(500)
			.append( "SessionImpl(" );
		if ( !isClosed() ) {
			buf.append(persistenceContext)
				.append(";")
				.append(actionQueue);
		}
		else {
			buf.append("<closed>");
		}
		return buf.append(')').toString();
	}

	public EventListeners getListeners() {
		return listeners;
	}

	public ActionQueue getActionQueue() {
		errorIfClosed();
		checkTransactionSynchStatus();
		return actionQueue;
	}
	
	public PersistenceContext getPersistenceContext() {
		errorIfClosed();
		checkTransactionSynchStatus();
		return persistenceContext;
	}
	
	public SessionStatistics getStatistics() {
		checkTransactionSynchStatus();
		return new SessionStatisticsImpl(this);
	}

	public boolean isEventSource() {
		checkTransactionSynchStatus();
		return true;
	}

	public void setReadOnly(Object entity, boolean readOnly) {
		errorIfClosed();
		checkTransactionSynchStatus();
		persistenceContext.setReadOnly(entity, readOnly);
	}

	public void afterScrollOperation() {
		// nothing to do in a stateful session
	}

	public String getFetchProfile() {
		checkTransactionSynchStatus();
		return fetchProfile;
	}

	public JDBCContext getJDBCContext() {
		errorIfClosed();
		checkTransactionSynchStatus();
		return jdbcContext;
	}

	public void setFetchProfile(String fetchProfile) {
		errorIfClosed();
		checkTransactionSynchStatus();
		this.fetchProfile = fetchProfile;
	}

	private void checkTransactionSynchStatus() {
		if ( jdbcContext != null && !isClosed() ) {
			jdbcContext.registerSynchronizationIfPossible();
		}
	}

	/**
	 * Used by JDK serialization...
	 *
	 * @param ois The input stream from which we are being read...
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
		log.trace( "deserializing session" );

		boolean isRootSession = ois.readBoolean();
		interceptor = ( Interceptor ) ois.readObject();
		factory = SessionFactoryImpl.deserialize( ois );
		listeners = factory.getEventListeners();
		if ( isRootSession ) {
			jdbcContext = JDBCContext.deserialize( ois, this, interceptor );
		}
		persistenceContext = StatefulPersistenceContext.deserialize( ois, this );
		actionQueue = ActionQueue.deserialize( ois, this );
		entityMode = EntityMode.parse( ( String ) ois.readObject() );
		autoClear = ois.readBoolean();
		flushMode = FlushMode.parse( ( String ) ois.readObject() );
		cacheMode = CacheMode.parse( ( String ) ois.readObject() );
		flushBeforeCompletionEnabled = ois.readBoolean();
		autoCloseSessionEnabled = ois.readBoolean();
		connectionReleaseMode = ConnectionReleaseMode.parse( ( String ) ois.readObject() );
		fetchProfile = ( String ) ois.readObject();
		enabledFilters = ( Map ) ois.readObject();
		childSessionsByEntityMode = ( Map ) ois.readObject();

		Iterator iter = enabledFilters.values().iterator();
		while ( iter.hasNext() ) {
			( ( FilterImpl ) iter.next() ).afterDeserialize(factory);
		}

		if ( isRootSession && childSessionsByEntityMode != null ) {
			iter = childSessionsByEntityMode.values().iterator();
			while ( iter.hasNext() ) {
				final SessionImpl child = ( ( SessionImpl ) iter.next() );
				child.rootSession = this;
				child.jdbcContext = this.jdbcContext;
			}
		}
	}

	/**
	 * Used by JDK serialization...
	 *
	 * @param oos The output stream to which we are being written...
	 * @throws IOException
	 */
	private void writeObject(ObjectOutputStream oos) throws IOException {
		if ( !jdbcContext.getConnectionManager().isReadyForSerialization() ) {
			throw new IllegalStateException( "Cannot serialize a session while connected" );
		}

		log.trace( "serializing session" );

		oos.writeBoolean( rootSession == null );
		// we need to writeObject() on this since interceptor is user defined
		oos.writeObject( interceptor );
		factory.serialize( oos );
		if ( rootSession == null ) {
			jdbcContext.serialize( oos );
		}
		persistenceContext.serialize( oos );
		actionQueue.serialize( oos );
		oos.writeObject( entityMode.toString() );
		oos.writeBoolean( autoClear );
		oos.writeObject( flushMode.toString() );
		oos.writeObject( cacheMode.toString() );
		oos.writeBoolean( flushBeforeCompletionEnabled );
		oos.writeBoolean( autoCloseSessionEnabled );
		oos.writeObject( connectionReleaseMode.toString() );
		oos.writeObject( fetchProfile );
		// todo : look at optimizing these...
		oos.writeObject( enabledFilters );
		oos.writeObject( childSessionsByEntityMode );
	}
}

⌨️ 快捷键说明

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