Top Description Fields Constructors Methods
org.hibernate.proxy

public abstract Class AbstractLazyInitializer

extends Object
implements LazyInitializer
Class Inheritance
All Implemented Interfaces
org.hibernate.proxy.LazyInitializer
Known Direct Subclasses
org.hibernate.proxy.map.MapLazyInitializer, org.hibernate.proxy.pojo.BasicLazyInitializer
Imports
org.hibernate.FlushMode, .HibernateException, .LazyInitializationException, .SessionException, .TransientObjectException, org.hibernate.boot.spi.SessionFactoryOptions, org.hibernate.engine.spi.EntityKey, .PersistenceContext, .SessionFactoryImplementor, .SharedSessionContractImplementor, org.hibernate.internal.CoreLogging, .CoreMessageLogger, .SessionFactoryRegistry, org.hibernate.persister.entity.EntityPersister

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

Field Summary

Modifier and TypeField and Description
private boolean
private String
private Object
private boolean
private static final CoreMessageLogger
private boolean
private Boolean
private transient SharedSessionContractImplementor
private String
private String
private Object
private boolean

Constructor Summary

AccessConstructor and Description
protected
AbstractLazyInitializer(String
The name of the entity being proxied.
entityName
,
Object
The identifier of the entity being proxied.
id
,
SharedSessionContractImplementor
The session owning the proxy.
session
)

Main constructor.

Method Summary

Modifier and TypeMethod and Description
pack-priv final void
afterDeserialization(Boolean
the read-only/modifiable setting to use when associated with a session; null indicates that the default should be used.
readOnlyBeforeAttachedToSession
,
String
the session factory uuid, to be used if allowLoadOutsideTransaction is true.
sessionFactoryUuid
,
String sessionFactoryName, boolean
whether the proxy can load data even if it's not attached to a session with an ongoing transaction.
allowLoadOutsideTransaction
)

Restore settings that are not passed to the constructor, but are still preserved during serialization.

private void
private void
private static EntityKey
public final String
getEntityName()

Implements org.hibernate.proxy.LazyInitializer.getEntityName.

The entity-name of the entity our owning proxy represents.
public final Object
getIdentifier()

Implements org.hibernate.proxy.LazyInitializer.getIdentifier.

Retrieve the identifier value for the entity our owning proxy represents, without initializing the proxy.
public final Object
getImplementation()

Implements org.hibernate.proxy.LazyInitializer.getImplementation.

Return the underlying persistent object, initializing if necessary
public final Object
getImplementation(SharedSessionContractImplementor
The session to check
s
)

Implements org.hibernate.proxy.LazyInitializer.getImplementation.

Return the underlying persistent object in the given session, or null if not contained in this session's persistence context.
public String
getImplementationEntityName()

Implements org.hibernate.proxy.LazyInitializer.getImplementationEntityName.

Get the actual name of the entity, possibly initializing the entity if it has subclasses.
public final Object
getInternalIdentifier()

Overrides default org.hibernate.proxy.LazyInitializer.getInternalIdentifier.

Retrieve the identifier value for the entity our owning proxy represents.
private Object
public final SharedSessionContractImplementor
getSession()

Implements org.hibernate.proxy.LazyInitializer.getSession.

Get the session to which this proxy is associated, or null if it is not attached.
protected String

Returns:

the session factory name.
getSessionFactoryName
()

Get the session factory name.

protected String

Returns:

the session factory UUID.
getSessionFactoryUuid
()

Get the session factory UUID.

protected final Object

Returns:

Value for property 'target'.
getTarget
()

Getter for property 'target'.

public final void
initialize()

Implements org.hibernate.proxy.LazyInitializer.initialize.

Initialize the proxy, fetching the target entity if necessary.
public final void
initializeWithoutLoadIfPossible()

Attempt to initialize the proxy without loading anything from the database.

protected boolean

Returns:

true if out-of-transaction loads are allowed, false otherwise.
isAllowLoadOutsideTransaction
()

Get whether the proxy can load data even if it's not attached to a session with an ongoing transaction.

protected final boolean

Returns:

Value for property 'connectedToSession'.
isConnectedToSession
()

Getter for property 'connectedToSession'.

private boolean
public final boolean
isReadOnly()

Implements org.hibernate.proxy.LazyInitializer.isReadOnly.

Is the proxy read-only?
public final Boolean

Returns:

null, if the default setting should be used; true, for read-only; false, for modifiable
isReadOnlyBeforeAttachedToSession
()

Get the read-only/modifiable setting that should be put in affect when it is attached to a session.

public final boolean
isReadOnlySettingAvailable()

Implements org.hibernate.proxy.LazyInitializer.isReadOnlySettingAvailable.

Is the proxy's read-only/modifiable setting available?
public final boolean
public boolean
protected void
protected void
prepareForPossibleLoadingOutsideTransaction()

Initialize internal state based on the currently attached session, in order to be ready to load data even after the proxy is detached from the session.

public final void
setIdentifier(Object
The identifier value.
id
)

Implements org.hibernate.proxy.LazyInitializer.setIdentifier.

Set the identifier value for the entity our owning proxy represents.
public final void
setImplementation(Object
The proxy target (the actual entity being proxied).
target
)

Implements org.hibernate.proxy.LazyInitializer.setImplementation.

Initialize the proxy manually by injecting its target.
public final void
setReadOnly(boolean
if true, the associated proxy is made read-only; if false, the associated proxy is made modifiable.
readOnly
)

Implements org.hibernate.proxy.LazyInitializer.setReadOnly.

Set an associated modifiable proxy to read-only mode, or a read-only proxy to modifiable mode.
public final void
setSession(SharedSessionContractImplementor
The session
s
)

Implements org.hibernate.proxy.LazyInitializer.setSession.

Associate the proxy with the given session.
public void
public final void
unsetSession()

Implements org.hibernate.proxy.LazyInitializer.unsetSession.

Unset this initializer's reference to session.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowLoadOutsideTransactionback to summary
private boolean allowLoadOutsideTransaction
entityNameback to summary
private String entityName
idback to summary
private Object id
initializedback to summary
private boolean initialized
LOGback to summary
private static final CoreMessageLogger LOG
readOnlyback to summary
private boolean readOnly
readOnlyBeforeAttachedToSessionback to summary
private Boolean readOnlyBeforeAttachedToSession
sessionback to summary
private transient SharedSessionContractImplementor session
sessionFactoryNameback to summary
private String sessionFactoryName
sessionFactoryUuidback to summary
private String sessionFactoryUuid
targetback to summary
private Object target
unwrapback to summary
private boolean unwrap

Constructor Detail

AbstractLazyInitializerback to summary
protected AbstractLazyInitializer(String entityName, Object id, SharedSessionContractImplementor session)

Main constructor.

Parameters
entityName:String

The name of the entity being proxied.

id:Object

The identifier of the entity being proxied.

session:SharedSessionContractImplementor

The session owning the proxy.

Method Detail

afterDeserializationback to summary
pack-priv final void afterDeserialization(Boolean readOnlyBeforeAttachedToSession, String sessionFactoryUuid, String sessionFactoryName, boolean allowLoadOutsideTransaction)

Restore settings that are not passed to the constructor, but are still preserved during serialization. This method should only be called during deserialization, before associating the proxy with a session.

Parameters
readOnlyBeforeAttachedToSession:Boolean

the read-only/modifiable setting to use when associated with a session; null indicates that the default should be used.

sessionFactoryUuid:String

the session factory uuid, to be used if allowLoadOutsideTransaction is true.

allowLoadOutsideTransaction:boolean

whether the proxy can load data even if it's not attached to a session with an ongoing transaction.

Exceptions
IllegalStateException:
if isReadOnlySettingAvailable() == true
checkTargetStateback to summary
private void checkTargetState(SharedSessionContractImplementor session)
errorIfReadOnlySettingNotAvailableback to summary
private void errorIfReadOnlySettingNotAvailable()
generateEntityKeyOrNullback to summary
private static EntityKey generateEntityKeyOrNull(Object id, SharedSessionContractImplementor s, String entityName)
getEntityNameback to summary
public final String getEntityName()

Implements org.hibernate.proxy.LazyInitializer.getEntityName.

Doc from org.hibernate.proxy.LazyInitializer.getEntityName.

The entity-name of the entity our owning proxy represents.

Returns:String

The entity-name.

Annotations
@Override
getIdentifierback to summary
public final Object getIdentifier()

Implements org.hibernate.proxy.LazyInitializer.getIdentifier.

Doc from org.hibernate.proxy.LazyInitializer.getIdentifier.

Retrieve the identifier value for the entity our owning proxy represents, without initializing the proxy.

When JPA proxy compliance is enabled the proxy is initialized.

Returns:Object

The identifier value.

Annotations
@Override
getImplementationback to summary
public final Object getImplementation()

Implements org.hibernate.proxy.LazyInitializer.getImplementation.

Doc from org.hibernate.proxy.LazyInitializer.getImplementation.

Return the underlying persistent object, initializing if necessary

Returns:Object

The underlying target entity.

Annotations
@Override
getImplementationback to summary
public final Object getImplementation(SharedSessionContractImplementor s) throws HibernateException

Implements org.hibernate.proxy.LazyInitializer.getImplementation.

Doc from org.hibernate.proxy.LazyInitializer.getImplementation.

Return the underlying persistent object in the given session, or null if not contained in this session's persistence context.

Parameters
s:SharedSessionContractImplementor

The session to check

Returns:Object

The target, or null.

Annotations
@Override
Exceptions
HibernateException:
Indicates problem locating the target.
getImplementationEntityNameback to summary
public String getImplementationEntityName()

Implements org.hibernate.proxy.LazyInitializer.getImplementationEntityName.

Doc from org.hibernate.proxy.LazyInitializer.getImplementationEntityName.

Get the actual name of the entity, possibly initializing the entity if it has subclasses.

Returns:String

The actual entity name.

Annotations
@Override
getInternalIdentifierback to summary
public final Object getInternalIdentifier()

Overrides default org.hibernate.proxy.LazyInitializer.getInternalIdentifier.

Doc from org.hibernate.proxy.LazyInitializer.getInternalIdentifier.

Retrieve the identifier value for the entity our owning proxy represents.

Returns:Object

The identifier value.

Annotations
@Override
getProxyOrNullback to summary
private Object getProxyOrNull()
getSessionback to summary
public final SharedSessionContractImplementor getSession()

Implements org.hibernate.proxy.LazyInitializer.getSession.

Doc from org.hibernate.proxy.LazyInitializer.getSession.

Get the session to which this proxy is associated, or null if it is not attached.

Returns:SharedSessionContractImplementor

The associated session.

Annotations
@Override
getSessionFactoryNameback to summary
protected String getSessionFactoryName()

Get the session factory name. This method should only be called during serialization, and only makes sense after a call to prepareForPossibleLoadingOutsideTransaction().

Returns:String

the session factory name.

getSessionFactoryUuidback to summary
protected String getSessionFactoryUuid()

Get the session factory UUID. This method should only be called during serialization, and only makes sense after a call to prepareForPossibleLoadingOutsideTransaction().

Returns:String

the session factory UUID.

getTargetback to summary
protected final Object getTarget()

Getter for property 'target'.

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

Returns:Object

Value for property 'target'.

initializeback to summary
public final void initialize() throws HibernateException

Implements org.hibernate.proxy.LazyInitializer.initialize.

Doc from org.hibernate.proxy.LazyInitializer.initialize.

Initialize the proxy, fetching the target entity if necessary.

Annotations
@Override
Exceptions
HibernateException:
Indicates a problem initializing the proxy.
initializeWithoutLoadIfPossibleback to summary
public final void initializeWithoutLoadIfPossible()

Attempt to initialize the proxy without loading anything from the database. This will only have any effect if the proxy is still attached to a session, and the entity being proxied has been loaded and added to the persistence context of that session since the proxy was created.

isAllowLoadOutsideTransactionback to summary
protected boolean isAllowLoadOutsideTransaction()

Get whether the proxy can load data even if it's not attached to a session with an ongoing transaction. This method should only be called during serialization, and only makes sense after a call to prepareForPossibleLoadingOutsideTransaction().

Returns:boolean

true if out-of-transaction loads are allowed, false otherwise.

isConnectedToSessionback to summary
protected final boolean isConnectedToSession()

Getter for property 'connectedToSession'.

Returns:boolean

Value for property 'connectedToSession'.

isInitializeProxyWhenAccessingIdentifierback to summary
private boolean isInitializeProxyWhenAccessingIdentifier()
isReadOnlyback to summary
public final boolean isReadOnly()

Implements org.hibernate.proxy.LazyInitializer.isReadOnly.

Doc from org.hibernate.proxy.LazyInitializer.isReadOnly.

Is the proxy read-only?

The read-only/modifiable setting is not available when the proxy is detached or its associated session is closed.

To check if the read-only/modifiable setting is available:

Returns:boolean

true, if this proxy is read-only; false, otherwise

Annotations
@Override
isReadOnlyBeforeAttachedToSessionback to summary
public final Boolean isReadOnlyBeforeAttachedToSession()

Get the read-only/modifiable setting that should be put in affect when it is attached to a session.

This method should only be called during serialization when read-only/modifiable setting is not available (i.e., isReadOnlySettingAvailable() == false)

Returns:Boolean

null, if the default setting should be used; true, for read-only; false, for modifiable

Exceptions
IllegalStateException:
if isReadOnlySettingAvailable() == true
isReadOnlySettingAvailableback to summary
public final boolean isReadOnlySettingAvailable()

Implements org.hibernate.proxy.LazyInitializer.isReadOnlySettingAvailable.

Doc from org.hibernate.proxy.LazyInitializer.isReadOnlySettingAvailable.

Is the proxy's read-only/modifiable setting available?

Returns:boolean

true, if the setting is available false, if the proxy is detached or its associated session is closed

Annotations
@Override
isUninitializedback to summary
public final boolean isUninitialized()

Implements org.hibernate.proxy.LazyInitializer.isUninitialized.

Doc from org.hibernate.proxy.LazyInitializer.isUninitialized.

Is the proxy uninitialized?

Returns:boolean

True if uninitialized; false otherwise.

Annotations
@Override
isUnwrapback to summary
public boolean isUnwrap()

Implements org.hibernate.proxy.LazyInitializer.isUnwrap.

Annotations
@Override
permissiveInitializationback to summary
protected void permissiveInitialization()
prepareForPossibleLoadingOutsideTransactionback to summary
protected void prepareForPossibleLoadingOutsideTransaction()

Initialize internal state based on the currently attached session, in order to be ready to load data even after the proxy is detached from the session.

setIdentifierback to summary
public final void setIdentifier(Object id)

Implements org.hibernate.proxy.LazyInitializer.setIdentifier.

Doc from org.hibernate.proxy.LazyInitializer.setIdentifier.

Set the identifier value for the entity our owning proxy represents.

Parameters
id:Object

The identifier value.

Annotations
@Override
setImplementationback to summary
public final void setImplementation(Object target)

Implements org.hibernate.proxy.LazyInitializer.setImplementation.

Doc from org.hibernate.proxy.LazyInitializer.setImplementation.

Initialize the proxy manually by injecting its target.

Parameters
target:Object

The proxy target (the actual entity being proxied).

Annotations
@Override
setReadOnlyback to summary
public final void setReadOnly(boolean readOnly)

Implements org.hibernate.proxy.LazyInitializer.setReadOnly.

Doc from org.hibernate.proxy.LazyInitializer.setReadOnly.

Set an associated modifiable proxy to read-only mode, or a read-only proxy to modifiable mode. If the proxy is currently initialized, its implementation will be set to the same mode; otherwise, when the proxy is initialized, its implementation will have the same read-only/ modifiable setting as the proxy. In read-only mode, no snapshot is maintained and the instance is never dirty checked.

If the associated proxy already has the specified read-only/modifiable setting, then this method does nothing.

Parameters
readOnly:boolean

if true, the associated proxy is made read-only; if false, the associated proxy is made modifiable.

Annotations
@Override
setSessionback to summary
public final void setSession(SharedSessionContractImplementor s) throws HibernateException

Implements org.hibernate.proxy.LazyInitializer.setSession.

Doc from org.hibernate.proxy.LazyInitializer.setSession.

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 symmetry of the association. That must be done separately 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 context.

Parameters
s:SharedSessionContractImplementor

The session

Annotations
@Override
Exceptions
HibernateException:
Indicates that the proxy was still contained in the persistence context of the "previous session".
setUnwrapback to summary
public void setUnwrap(boolean unwrap)

Implements org.hibernate.proxy.LazyInitializer.setUnwrap.

Annotations
@Override
unsetSessionback to summary
public final void unsetSession()

Implements org.hibernate.proxy.LazyInitializer.unsetSession.

Doc from org.hibernate.proxy.LazyInitializer.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 org.hibernate.Session#evict and org.hibernate.Session#clear processing; most other use-cases should call setSession instead.

Annotations
@Override