KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > proxy > AbstractLazyInitializer


1 //$Id: AbstractLazyInitializer.java,v 1.11 2005/06/20 20:32:35 oneovthafew Exp $
2
package org.hibernate.proxy;
3
4 import java.io.Serializable JavaDoc;
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 /**
12  * @author Gavin King
13  */

14 public abstract class AbstractLazyInitializer implements LazyInitializer {
15     
16     private Object JavaDoc target;
17     private String JavaDoc entityName;
18     private Serializable JavaDoc id;
19     private transient SessionImplementor session;
20     private boolean unwrap;
21     
22     protected AbstractLazyInitializer(String JavaDoc entityName, Serializable JavaDoc id, SessionImplementor session) {
23         this.id = id;
24         this.session = session;
25         this.entityName = entityName;
26     }
27
28     public final Serializable JavaDoc getIdentifier() {
29         return id;
30     }
31
32     public final void setIdentifier(Serializable JavaDoc id) {
33         this.id = id;
34     }
35
36     public final String JavaDoc 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                 //TODO: perhaps this should be some other RuntimeException...
69
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 JavaDoc target) {
78         this.target = target;
79     }
80
81     /**
82      * Return the underlying persistent object, initializing if necessary
83      */

84     public final Object JavaDoc getImplementation() {
85         initialize();
86         return target;
87     }
88
89     /**
90      * Return the underlying persistent object in the given <tt>Session</tt>, or null,
91      * do not initialize the proxy
92      */

93     public final Object JavaDoc 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 JavaDoc target) {
102         this.target = target;
103     }
104
105     protected final Object JavaDoc 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