org.hibernate.proxy
Class AbstractLazyInitializer

java.lang.Object
  extended byorg.hibernate.proxy.AbstractLazyInitializer
All Implemented Interfaces:
LazyInitializer
Direct Known Subclasses:
BasicLazyInitializer, Dom4jLazyInitializer, MapLazyInitializer

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

Constructor Summary
protected AbstractLazyInitializer()
          For serialization from the non-pojo initializers (HHH-3309)
protected AbstractLazyInitializer(String entityName, Serializable id, SessionImplementor session)
          Main constructor.
 
Method Summary
 String getEntityName()
          The entity-name of the entity our owning proxy represents.
 Serializable getIdentifier()
          Retrieve the identifier value for the enity our owning proxy represents.
 Object getImplementation()
          Return the underlying persistent object, initializing if necessary
 Object getImplementation(SessionImplementor s)
          Return the underlying persistent object in the given Session, or null, do not initialize the proxy
 SessionImplementor getSession()
          Get the session to which this proxy is associated, or null if it is not attached.
protected  Object getTarget()
          Getter for property 'target'.
 void initialize()
          Initialize the proxy, fetching the target entity if necessary.
protected  boolean isConnectedToSession()
          Getter for property 'connectedToSession'.
 boolean isUninitialized()
          Is the proxy uninitialzed?
 boolean isUnwrap()
          
 void setIdentifier(Serializable id)
          Set the identifier value for the enity our owning proxy represents.
 void setImplementation(Object target)
          Initialize the proxy manually by injecting its target.
 void setSession(SessionImplementor s)
          Associate the proxy with the given session.

Care should be given to make certain that the proxy is added to the session's persistence context as well to maintain the symetry of the association. That must be done seperately as this method simply sets an internal reference. We do also check that if there is already an associated session that the proxy reference was removed from that previous session's persistence contet.

 void setUnwrap(boolean unwrap)
          
 void unsetSession()
          Unset this initializer's reference to session. It is assumed that the caller is also taking care or cleaning up the owning proxy's reference in the persistence context.

Generally speaking this is intended to be called only during Session.evict(java.lang.Object) and Session.clear() processing; most other use-cases should call LazyInitializer.setSession(org.hibernate.engine.SessionImplementor) instead.

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.hibernate.proxy.LazyInitializer
getPersistentClass
 

Constructor Detail

AbstractLazyInitializer

protected AbstractLazyInitializer()
For serialization from the non-pojo initializers (HHH-3309)


AbstractLazyInitializer

protected AbstractLazyInitializer(String entityName,
                                  Serializable id,
                                  SessionImplementor session)
Main constructor.

Parameters:
entityName - The name of the entity being proxied.
id - The identifier of the entity being proxied.
session - The session owning the proxy.
Method Detail

getEntityName

public final String getEntityName()
The entity-name of the entity our owning proxy represents.

Specified by:
getEntityName in interface LazyInitializer
Returns:
The entity-name.

getIdentifier

public final Serializable getIdentifier()
Retrieve the identifier value for the enity our owning proxy represents.

Specified by:
getIdentifier in interface LazyInitializer
Returns:
The identifier value.

setIdentifier

public final void setIdentifier(Serializable id)
Set the identifier value for the enity our owning proxy represents.

Specified by:
setIdentifier in interface LazyInitializer
Parameters:
id - The identifier value.

isUninitialized

public final boolean isUninitialized()
Is the proxy uninitialzed?

Specified by:
isUninitialized in interface LazyInitializer
Returns:
True if uninitialized; false otherwise.

getSession

public final SessionImplementor getSession()
Get the session to which this proxy is associated, or null if it is not attached.

Specified by:
getSession in interface LazyInitializer
Returns:
The associated session.

setSession

public final void setSession(SessionImplementor s)
                      throws HibernateException
Associate the proxy with the given session.

Care should be given to make certain that the proxy is added to the session's persistence context as well to maintain the symetry of the association. That must be done seperately as this method simply sets an internal reference. We do also check that if there is already an associated session that the proxy reference was removed from that previous session's persistence contet.

Specified by:
setSession in interface LazyInitializer
Parameters:
s - The session
Throws:
HibernateException - Indicates that the proxy was still contained in the persistence context of the "previous session".

unsetSession

public void unsetSession()
Unset this initializer's reference to session. It is assumed that the caller is also taking care or cleaning up the owning proxy's reference in the persistence context.

Generally speaking this is intended to be called only during Session.evict(java.lang.Object) and Session.clear() processing; most other use-cases should call LazyInitializer.setSession(org.hibernate.engine.SessionImplementor) instead.

Specified by:
unsetSession in interface LazyInitializer

initialize

public final void initialize()
                      throws HibernateException
Initialize the proxy, fetching the target entity if necessary.

Specified by:
initialize in interface LazyInitializer
Throws:
HibernateException - Indicates a problem initializing the proxy.

isConnectedToSession

protected final boolean isConnectedToSession()
Getter for property 'connectedToSession'.

Returns:
Value for property 'connectedToSession'.

getImplementation

public final Object getImplementation()
Return the underlying persistent object, initializing if necessary

Specified by:
getImplementation in interface LazyInitializer
Returns:
The underlying target entity.

setImplementation

public final void setImplementation(Object target)
Initialize the proxy manually by injecting its target.

Specified by:
setImplementation in interface LazyInitializer
Parameters:
target - The proxy target (the actual entity being proxied).

getImplementation

public final Object getImplementation(SessionImplementor s)
                               throws HibernateException
Return the underlying persistent object in the given Session, or null, do not initialize the proxy

Specified by:
getImplementation in interface LazyInitializer
Parameters:
s - The session to check
Returns:
The target, or null.
Throws:
HibernateException - Indicates problem locating the target.

getTarget

protected final Object getTarget()
Getter for property 'target'.

Same as getImplementation() except that this method will not force initialization.

Returns:
Value for property 'target'.

isUnwrap

public boolean isUnwrap()

Specified by:
isUnwrap in interface LazyInitializer

setUnwrap

public void setUnwrap(boolean unwrap)

Specified by:
setUnwrap in interface LazyInitializer