1
package org.hibernate.proxy;
3
import java.io.Serializable;
5
import org.hibernate.HibernateException;
6
import org.hibernate.LazyInitializationException;
7
import org.hibernate.ObjectNotFoundException;
8
import org.hibernate.engine.EntityKey;
9
import org.hibernate.engine.SessionImplementor;
12
* Convenience base class for lazy initialization handlers. Centralizes the
13
* basic plumbing of doing lazy initialization freeing subclasses to
14
* acts as essentially adapters to their intended entity mode and/or
15
* proxy generation strategy.
19
public abstract class AbstractLazyInitializer implements LazyInitializer {
21
private Object target;
22
private boolean initialized;
23
private String entityName;
24
private Serializable id;
25
private transient SessionImplementor session;
26
private boolean unwrap;
28
protected AbstractLazyInitializer(String entityName, Serializable id, SessionImplementor session) {
30
this.session = session;
31
this.entityName = entityName;
34
public final Serializable getIdentifier() {
38
public final void setIdentifier(Serializable id) {
42
public final String getEntityName() {
46
public final boolean isUninitialized() {
50
public final SessionImplementor getSession() {
54
public final void initialize() throws HibernateException {
56
if ( session==null ) {
57
throw new LazyInitializationException("could not initialize proxy - no Session");
59
else if ( !session.isOpen() ) {
60
throw new LazyInitializationException("could not initialize proxy - the owning Session was closed");
62
else if ( !session.isConnected() ) {
63
throw new LazyInitializationException("could not initialize proxy - the owning Session is disconnected");
66
target = session.immediateLoad(entityName, id);
76
private void checkTargetState() {
78
if ( target == null ) {
79
getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id );
84
public final void setSession(SessionImplementor s) throws HibernateException {
86
if ( isConnectedToSession() ) {
87
//TODO: perhaps this should be some other RuntimeException...
88
throw new HibernateException("illegally attempted to associate a proxy with two open Sessions");
96
protected final boolean isConnectedToSession() {
97
return session!=null &&
99
session.getPersistenceContext().containsProxy(this);
102
public final void setImplementation(Object target) {
103
this.target = target;
108
* Return the underlying persistent object, initializing if necessary
110
public final Object getImplementation() {
116
* Return the underlying persistent object in the given <tt>Session</tt>, or null,
117
* do not initialize the proxy
119
public final Object getImplementation(SessionImplementor s) throws HibernateException {
120
final EntityKey entityKey = new EntityKey(
122
s.getFactory().getEntityPersister( getEntityName() ),
125
return s.getPersistenceContext().getEntity( entityKey );
128
protected final Object getTarget() {
132
public boolean isUnwrap() {
136
public void setUnwrap(boolean unwrap) {
137
this.unwrap = unwrap;