FileDocCategorySizeDatePackage
AbstractLazyInitializer.javaAPI DocHibernate 3.2.53574Tue May 02 13:55:46 BST 2006org.hibernate.proxy

AbstractLazyInitializer

public abstract class AbstractLazyInitializer extends Object implements LazyInitializer
Convenience base class for lazy initialization handlers. Centralizes the basic plumbing of doing lazy initialization freeing subclasses to acts as essentially adapters to their intended entity mode and/or proxy generation strategy.
author
Gavin King

Fields Summary
private Object
target
private boolean
initialized
private String
entityName
private Serializable
id
private transient org.hibernate.engine.SessionImplementor
session
private boolean
unwrap
Constructors Summary
protected AbstractLazyInitializer(String entityName, Serializable id, org.hibernate.engine.SessionImplementor session)

		this.id = id;
		this.session = session;
		this.entityName = entityName;
	
Methods Summary
private voidcheckTargetState()

		if ( !unwrap ) {
			if ( target == null ) {
				getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id );
			}
		}
	
public final java.lang.StringgetEntityName()

		return entityName;
	
public final java.io.SerializablegetIdentifier()

		return id;
	
public final java.lang.ObjectgetImplementation()
Return the underlying persistent object, initializing if necessary

		initialize();
		return target;
	
public final java.lang.ObjectgetImplementation(org.hibernate.engine.SessionImplementor s)
Return the underlying persistent object in the given Session, or null, do not initialize the proxy

		final EntityKey entityKey = new EntityKey(
				getIdentifier(),
				s.getFactory().getEntityPersister( getEntityName() ),
				s.getEntityMode()
			);
		return s.getPersistenceContext().getEntity( entityKey );
	
public final org.hibernate.engine.SessionImplementorgetSession()

		return session;
	
protected final java.lang.ObjectgetTarget()

		return target;
	
public final voidinitialize()

		if (!initialized) {
			if ( session==null ) {
				throw new LazyInitializationException("could not initialize proxy - no Session");
			}
			else if ( !session.isOpen() ) {
				throw new LazyInitializationException("could not initialize proxy - the owning Session was closed");
			}
			else if ( !session.isConnected() ) {
				throw new LazyInitializationException("could not initialize proxy - the owning Session is disconnected");
			}
			else {
				target = session.immediateLoad(entityName, id);
				initialized = true;
				checkTargetState();
			}
		}
		else {
			checkTargetState();
		}
	
protected final booleanisConnectedToSession()

		return session!=null && 
				session.isOpen() && 
				session.getPersistenceContext().containsProxy(this);
	
public final booleanisUninitialized()

		return !initialized;
	
public booleanisUnwrap()

		return unwrap;
	
public final voidsetIdentifier(java.io.Serializable id)

		this.id = id;
	
public final voidsetImplementation(java.lang.Object target)

		this.target = target;
		initialized = true;
	
public final voidsetSession(org.hibernate.engine.SessionImplementor s)

		if (s!=session) {
			if ( isConnectedToSession() ) {
				//TODO: perhaps this should be some other RuntimeException...
				throw new HibernateException("illegally attempted to associate a proxy with two open Sessions");
			}
			else {
				session = s;
			}
		}
	
public voidsetUnwrap(boolean unwrap)

		this.unwrap = unwrap;