1 package org.hibernate.proxy; 3 4 import java.io.Serializable ; 5 6 import org.hibernate.HibernateException; 7 import org.hibernate.LazyInitializationException; 8 import org.hibernate.engine.EntityKey; 9 import org.hibernate.engine.SessionImplementor; 10 11 14 public abstract class AbstractLazyInitializer implements LazyInitializer { 15 16 private Object target; 17 private String entityName; 18 private Serializable id; 19 private transient SessionImplementor session; 20 private boolean unwrap; 21 22 protected AbstractLazyInitializer(String entityName, Serializable id, SessionImplementor session) { 23 this.id = id; 24 this.session = session; 25 this.entityName = entityName; 26 } 27 28 public final Serializable getIdentifier() { 29 return id; 30 } 31 32 public final void setIdentifier(Serializable id) { 33 this.id = id; 34 } 35 36 public final String getEntityName() { 37 return entityName; 38 } 39 40 public final boolean isUninitialized() { 41 return target == null; 42 } 43 44 public final SessionImplementor getSession() { 45 return session; 46 } 47 48 public final void initialize() throws HibernateException { 49 if (target==null) { 50 if ( session==null ) { 51 throw new LazyInitializationException("could not initialize proxy - no Session"); 52 } 53 else if ( !session.isOpen() ) { 54 throw new LazyInitializationException("could not initialize proxy - the owning Session was closed"); 55 } 56 else if ( !session.isConnected() ) { 57 throw new LazyInitializationException("could not initialize proxy - the owning Session is disconnected"); 58 } 59 else { 60 target = session.immediateLoad(entityName, id); 61 } 62 } 63 } 64 65 public final void setSession(SessionImplementor s) throws HibernateException { 66 if (s!=session) { 67 if ( session!=null && session.isOpen() ) { 68 throw new HibernateException("illegally attempted to associate a proxy with two open Sessions"); 70 } 71 else { 72 session = s; 73 } 74 } 75 } 76 77 public final void setImplementation(Object target) { 78 this.target = target; 79 } 80 81 84 public final Object getImplementation() { 85 initialize(); 86 return target; 87 } 88 89 93 public final Object getImplementation(SessionImplementor s) throws HibernateException { 94 return s.getPersistenceContext().getEntity( new EntityKey( 95 getIdentifier(), 96 s.getFactory().getEntityPersister( getEntityName() ), 97 s.getEntityMode() 98 ) ); 99 } 100 101 protected final void setTarget(Object target) { 102 this.target = target; 103 } 104 105 protected final Object getTarget() { 106 return target; 107 } 108 109 public boolean isUnwrap() { 110 return unwrap; 111 } 112 113 public void setUnwrap(boolean unwrap) { 114 this.unwrap = unwrap; 115 } 116 117 } 118 | Popular Tags |