FileDocCategorySizeDatePackage
StatelessSessionImpl.javaAPI DocHibernate 3.2.518339Thu Jun 15 00:21:06 BST 2006org.hibernate.impl

StatelessSessionImpl

public class StatelessSessionImpl extends AbstractSessionImpl implements org.hibernate.StatelessSession, JDBCContext.Context
author
Gavin King

Fields Summary
private static final Log
log
private org.hibernate.jdbc.JDBCContext
jdbcContext
private org.hibernate.engine.PersistenceContext
temporaryPersistenceContext
Constructors Summary
StatelessSessionImpl(Connection connection, SessionFactoryImpl factory)


	    
		super( factory );
		this.jdbcContext = new JDBCContext( this, connection, EmptyInterceptor.INSTANCE );
	
Methods Summary
public voidafterOperation(boolean success)

		if ( !jdbcContext.isTransactionInProgress() ) {
			jdbcContext.afterNontransactionalQuery(success);
		}
	
public voidafterScrollOperation()

		temporaryPersistenceContext.clear();
	
public voidafterTransactionBegin(org.hibernate.Transaction tx)

public voidafterTransactionCompletion(boolean successful, org.hibernate.Transaction tx)

protected booleanautoFlushIfRequired(java.util.Set querySpaces)

		// no auto-flushing to support in stateless session
		return false;
	
public voidbeforeTransactionCompletion(org.hibernate.Transaction tx)

public org.hibernate.TransactionbeginTransaction()

		errorIfClosed();
		Transaction result = getTransaction();
		result.begin();
		return result;
	
public java.lang.StringbestGuessEntityName(java.lang.Object object)

		if (object instanceof HibernateProxy) {
			object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation();
		}
		return guessEntityName(object);
	
public voidclose()

		managedClose();
	
public java.sql.Connectionconnection()

		errorIfClosed();
		return jdbcContext.borrowConnection();
	
public org.hibernate.CriteriacreateCriteria(java.lang.Class persistentClass, java.lang.String alias)

		errorIfClosed();
		return new CriteriaImpl( persistentClass.getName(), alias, this );
	
public org.hibernate.CriteriacreateCriteria(java.lang.String entityName, java.lang.String alias)

		errorIfClosed();
		return new CriteriaImpl(entityName, alias, this);
	
public org.hibernate.CriteriacreateCriteria(java.lang.Class persistentClass)

		errorIfClosed();
		return new CriteriaImpl( persistentClass.getName(), this );
	
public org.hibernate.CriteriacreateCriteria(java.lang.String entityName)

		errorIfClosed();
		return new CriteriaImpl(entityName, this);
	
public voiddelete(java.lang.Object entity)

		errorIfClosed();
		delete(null, entity);
	
public voiddelete(java.lang.String entityName, java.lang.Object entity)

		errorIfClosed();
		EntityPersister persister = getEntityPersister(entityName, entity);
		Serializable id = persister.getIdentifier(entity, EntityMode.POJO);
		Object version = persister.getVersion(entity, EntityMode.POJO);
		persister.delete(id, version, entity, this);
	
public intexecuteNativeUpdate(org.hibernate.engine.query.sql.NativeSQLQuerySpecification nativeSQLQuerySpecification, org.hibernate.engine.QueryParameters queryParameters)

		errorIfClosed();
		queryParameters.validateParameters();
		NativeSQLQueryPlan plan = getNativeSQLQueryPlan(nativeSQLQuerySpecification);

		boolean success = false;
		int result = 0;
		try {
			result = plan.performExecuteUpdate(queryParameters, this);
			success = true;
		} finally {
			afterOperation(success);
		}
		temporaryPersistenceContext.clear();
		return result;
	
public intexecuteUpdate(java.lang.String query, org.hibernate.engine.QueryParameters queryParameters)

		errorIfClosed();
		queryParameters.validateParameters();
		HQLQueryPlan plan = getHQLQueryPlan( query, false );
		boolean success = false;
		int result = 0;
		try {
			result = plan.performExecuteUpdate( queryParameters, this );
			success = true;
		}
		finally {
			afterOperation(success);
		}
		temporaryPersistenceContext.clear();
		return result;
	
public voidflush()

public java.lang.Objectget(java.lang.String entityName, java.io.Serializable id)

		return get(entityName, id, LockMode.NONE);
	
public java.lang.Objectget(java.lang.String entityName, java.io.Serializable id, org.hibernate.LockMode lockMode)

		errorIfClosed();
		Object result = getFactory().getEntityPersister(entityName)
				.load(id, null, lockMode, this);
		temporaryPersistenceContext.clear();
		return result;
	
public java.lang.Objectget(java.lang.Class entityClass, java.io.Serializable id)

		return get( entityClass.getName(), id );
	
public java.lang.Objectget(java.lang.Class entityClass, java.io.Serializable id, org.hibernate.LockMode lockMode)

		return get( entityClass.getName(), id, lockMode );
	
public org.hibernate.jdbc.BatchergetBatcher()

		errorIfClosed();
		return jdbcContext.getConnectionManager()
				.getBatcher();
	
public org.hibernate.CacheModegetCacheMode()

		return CacheMode.IGNORE;
	
public org.hibernate.ConnectionReleaseModegetConnectionReleaseMode()

		return factory.getSettings().getConnectionReleaseMode();
	
public java.io.SerializablegetContextEntityIdentifier(java.lang.Object object)

		errorIfClosed();
		return null;
	
public intgetDontFlushFromFind()

		return 0;
	
public java.util.MapgetEnabledFilters()

		return CollectionHelper.EMPTY_MAP;
	
public org.hibernate.EntityModegetEntityMode()

		return EntityMode.POJO;
	
public org.hibernate.persister.entity.EntityPersistergetEntityPersister(java.lang.String entityName, java.lang.Object object)

		errorIfClosed();
		if ( entityName==null ) {
			return factory.getEntityPersister( guessEntityName( object ) );
		}
		else {
			return factory.getEntityPersister( entityName )
					.getSubclassEntityPersister( object, getFactory(), EntityMode.POJO );
		}
	
public java.lang.ObjectgetEntityUsingInterceptor(org.hibernate.engine.EntityKey key)

		errorIfClosed();
		return null;
	
public java.lang.StringgetFetchProfile()

		return null;
	
public org.hibernate.type.TypegetFilterParameterType(java.lang.String filterParameterName)

		throw new UnsupportedOperationException();
	
public java.lang.ObjectgetFilterParameterValue(java.lang.String filterParameterName)

		throw new UnsupportedOperationException();
	
public org.hibernate.FlushModegetFlushMode()

		return FlushMode.COMMIT;
	
public org.hibernate.InterceptorgetInterceptor()

		return EmptyInterceptor.INSTANCE;
	
public org.hibernate.jdbc.JDBCContextgetJDBCContext()

		return jdbcContext;
	
public org.hibernate.event.EventListenersgetListeners()

		throw new UnsupportedOperationException();
	
private org.hibernate.persister.entity.OuterJoinLoadablegetOuterJoinLoadable(java.lang.String entityName)

		EntityPersister persister = factory.getEntityPersister(entityName);
		if ( !(persister instanceof OuterJoinLoadable) ) {
			throw new MappingException( "class persister is not OuterJoinLoadable: " + entityName );
		}
		return ( OuterJoinLoadable ) persister;
	
public org.hibernate.engine.PersistenceContextgetPersistenceContext()

		return temporaryPersistenceContext;
	
public longgetTimestamp()

		throw new UnsupportedOperationException();
	
public org.hibernate.TransactiongetTransaction()

		errorIfClosed();
		return jdbcContext.getTransaction();
	
public java.lang.StringguessEntityName(java.lang.Object entity)

		errorIfClosed();
		return entity.getClass().getName();
	
public java.lang.ObjectimmediateLoad(java.lang.String entityName, java.io.Serializable id)

		throw new SessionException("proxies cannot be fetched by a stateless session");
	
public voidinitializeCollection(org.hibernate.collection.PersistentCollection collection, boolean writing)

		throw new SessionException("collections cannot be fetched by a stateless session");
	
public java.io.Serializableinsert(java.lang.Object entity)

		errorIfClosed();
		return insert(null, entity);
	
public java.io.Serializableinsert(java.lang.String entityName, java.lang.Object entity)

		errorIfClosed();
		EntityPersister persister = getEntityPersister(entityName, entity);
		Serializable id = persister.getIdentifierGenerator().generate(this, entity);
		Object[] state = persister.getPropertyValues(entity, EntityMode.POJO);
		if ( persister.isVersioned() ) {
			boolean substitute = Versioning.seedVersion(state, persister.getVersionProperty(), persister.getVersionType(), this);
			if ( substitute ) {
				persister.setPropertyValues( entity, state, EntityMode.POJO );
			}
		}
		if ( id == IdentifierGeneratorFactory.POST_INSERT_INDICATOR ) {
			id = persister.insert(state, entity, this);
		}
		else {
			persister.insert(id, state, entity, this);
		}
		persister.setIdentifier(entity, id, EntityMode.POJO);
		return id;
	
public java.lang.Objectinstantiate(java.lang.String entityName, java.io.Serializable id)

		errorIfClosed();
		return getFactory().getEntityPersister( entityName )
				.instantiate( id, EntityMode.POJO );
	
public java.lang.ObjectinternalLoad(java.lang.String entityName, java.io.Serializable id, boolean eager, boolean nullable)

		errorIfClosed();
		EntityPersister persister = getFactory().getEntityPersister(entityName);
		if ( !eager && persister.hasProxy() ) {
			return persister.createProxy(id, this);
		}
		Object loaded = temporaryPersistenceContext.getEntity( new EntityKey(id, persister, EntityMode.POJO) );
		//TODO: if not loaded, throw an exception
		return loaded==null ? get( entityName, id ) : loaded;
	
public booleanisAutoCloseSessionEnabled()

		return factory.getSettings().isAutoCloseSessionEnabled();
	
public booleanisConnected()

		return jdbcContext.getConnectionManager().isCurrentlyConnected();
	
public booleanisEventSource()

		return false;
	
public booleanisFlushBeforeCompletionEnabled()

		return true;
	
public booleanisFlushModeNever()

		return false;
	
public booleanisOpen()

		return !isClosed();
	
public booleanisTransactionInProgress()

		return jdbcContext.isTransactionInProgress();
	
public java.util.Iteratoriterate(java.lang.String query, org.hibernate.engine.QueryParameters queryParameters)

		throw new UnsupportedOperationException();
	
public java.util.IteratoriterateFilter(java.lang.Object collection, java.lang.String filter, org.hibernate.engine.QueryParameters queryParameters)

		throw new UnsupportedOperationException();
	
public java.util.Listlist(java.lang.String query, org.hibernate.engine.QueryParameters queryParameters)

		errorIfClosed();
		queryParameters.validateParameters();
		HQLQueryPlan plan = getHQLQueryPlan( query, false );
		boolean success = false;
		List results = CollectionHelper.EMPTY_LIST;
		try {
			results = plan.performList( queryParameters, this );
			success = true;
		}
		finally {
			afterOperation(success);
		}
		temporaryPersistenceContext.clear();
		return results;
	
public java.util.Listlist(CriteriaImpl criteria)

		errorIfClosed();
		String[] implementors = factory.getImplementors( criteria.getEntityOrClassName() );
		int size = implementors.length;

		CriteriaLoader[] loaders = new CriteriaLoader[size];
		for( int i=0; i <size; i++ ) {
			loaders[i] = new CriteriaLoader(
					getOuterJoinLoadable( implementors[i] ),
			        factory,
			        criteria,
			        implementors[i],
			        getEnabledFilters()
			);
		}


		List results = Collections.EMPTY_LIST;
		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 {
			afterOperation(success);
		}
		temporaryPersistenceContext.clear();
		return results;
	
public java.util.ListlistCustomQuery(org.hibernate.loader.custom.CustomQuery customQuery, org.hibernate.engine.QueryParameters queryParameters)

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

		boolean success = false;
		List results;
		try {
			results = loader.list(this, queryParameters);
			success = true;
		}
		finally {
			afterOperation(success);
		}
		temporaryPersistenceContext.clear();
		return results;
	
public java.util.ListlistFilter(java.lang.Object collection, java.lang.String filter, org.hibernate.engine.QueryParameters queryParameters)

		throw new UnsupportedOperationException();
	
public voidmanagedClose()

		if ( isClosed() ) {
			throw new SessionException( "Session was already closed!" );
		}
		jdbcContext.getConnectionManager().close();
		setClosed();
	
public voidmanagedFlush()

		errorIfClosed();
		getBatcher().executeBatch();
	
public voidrefresh(java.lang.Object entity)

		refresh( bestGuessEntityName( entity ), entity, LockMode.NONE );
	
public voidrefresh(java.lang.String entityName, java.lang.Object entity)

		refresh( entityName, entity, LockMode.NONE );
	
public voidrefresh(java.lang.Object entity, org.hibernate.LockMode lockMode)

		refresh( bestGuessEntityName( entity ), entity, lockMode );
	
public voidrefresh(java.lang.String entityName, java.lang.Object entity, org.hibernate.LockMode lockMode)

		final EntityPersister persister = this.getEntityPersister( entityName, entity );
		final Serializable id = persister.getIdentifier( entity, getEntityMode() );
		if ( log.isTraceEnabled() ) {
			log.trace(
					"refreshing transient " +
					MessageHelper.infoString( persister, id, this.getFactory() )
			);
		}
		// TODO : can this ever happen???
//		EntityKey key = new EntityKey( id, persister, source.getEntityMode() );
//		if ( source.getPersistenceContext().getEntry( key ) != null ) {
//			throw new PersistentObjectException(
//					"attempted to refresh transient instance when persistent " +
//					"instance was already associated with the Session: " +
//					MessageHelper.infoString( persister, id, source.getFactory() )
//			);
//		}

		if ( persister.hasCache() ) {
			final CacheKey ck = new CacheKey(
					id,
			        persister.getIdentifierType(),
			        persister.getRootEntityName(),
			        this.getEntityMode(),
			        this.getFactory()
			);
			persister.getCache().remove(ck);
		}

		String previousFetchProfile = this.getFetchProfile();
		Object result = null;
		try {
			this.setFetchProfile( "refresh" );
			result = persister.load( id, entity, lockMode, this );
		}
		finally {
			this.setFetchProfile( previousFetchProfile );
		}
		UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() );
	
public org.hibernate.ScrollableResultsscroll(CriteriaImpl criteria, org.hibernate.ScrollMode scrollMode)

		errorIfClosed();
		String entityName = criteria.getEntityOrClassName();
		CriteriaLoader loader = new CriteriaLoader(
				getOuterJoinLoadable(entityName),
		        factory,
		        criteria,
		        entityName,
		        getEnabledFilters()
			);
		return loader.scroll(this, scrollMode);
	
public org.hibernate.ScrollableResultsscroll(java.lang.String query, org.hibernate.engine.QueryParameters queryParameters)

		errorIfClosed();
		HQLQueryPlan plan = getHQLQueryPlan( query, false );
		return plan.performScroll( queryParameters, this );
	
public org.hibernate.ScrollableResultsscrollCustomQuery(org.hibernate.loader.custom.CustomQuery customQuery, org.hibernate.engine.QueryParameters queryParameters)

		errorIfClosed();
		CustomLoader loader = new CustomLoader( customQuery, getFactory() );
		return loader.scroll(queryParameters, this);
	
public voidsetAutoClear(boolean enabled)

		throw new UnsupportedOperationException();
	
public voidsetCacheMode(org.hibernate.CacheMode cm)

		throw new UnsupportedOperationException();
	
public voidsetFetchProfile(java.lang.String name)

public voidsetFlushMode(org.hibernate.FlushMode fm)

		throw new UnsupportedOperationException();
	
public booleanshouldAutoClose()

		return isAutoCloseSessionEnabled() && !isClosed();
	
public voidupdate(java.lang.Object entity)

		errorIfClosed();
		update(null, entity);
	
public voidupdate(java.lang.String entityName, java.lang.Object entity)

		errorIfClosed();
		EntityPersister persister = getEntityPersister(entityName, entity);
		Serializable id = persister.getIdentifier(entity, EntityMode.POJO);
		Object[] state = persister.getPropertyValues(entity, EntityMode.POJO);
		Object oldVersion;
		if ( persister.isVersioned() ) {
			oldVersion = persister.getVersion(entity, EntityMode.POJO);
			Object newVersion = Versioning.increment( oldVersion, persister.getVersionType(), this );
			Versioning.setVersion(state, newVersion, persister);
			persister.setPropertyValues(entity, state, EntityMode.POJO);
		}
		else {
			oldVersion = null;
		}
		persister.update(id, state, null, false, null, oldVersion, entity, null, this);