~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/proxy/AbstractLazyInitializer.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hibernate.proxy;
 
2
 
 
3
import java.io.Serializable;
 
4
 
 
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;
 
10
 
 
11
/**
 
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.
 
16
 *
 
17
 * @author Gavin King
 
18
 */
 
19
public abstract class AbstractLazyInitializer implements LazyInitializer {
 
20
        
 
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;
 
27
        
 
28
        protected AbstractLazyInitializer(String entityName, Serializable id, SessionImplementor session) {
 
29
                this.id = id;
 
30
                this.session = session;
 
31
                this.entityName = entityName;
 
32
        }
 
33
 
 
34
        public final Serializable getIdentifier() {
 
35
                return id;
 
36
        }
 
37
 
 
38
        public final void setIdentifier(Serializable id) {
 
39
                this.id = id;
 
40
        }
 
41
 
 
42
        public final String getEntityName() {
 
43
                return entityName;
 
44
        }
 
45
 
 
46
        public final boolean isUninitialized() {
 
47
                return !initialized;
 
48
        }
 
49
 
 
50
        public final SessionImplementor getSession() {
 
51
                return session;
 
52
        }
 
53
 
 
54
        public final void initialize() throws HibernateException {
 
55
                if (!initialized) {
 
56
                        if ( session==null ) {
 
57
                                throw new LazyInitializationException("could not initialize proxy - no Session");
 
58
                        }
 
59
                        else if ( !session.isOpen() ) {
 
60
                                throw new LazyInitializationException("could not initialize proxy - the owning Session was closed");
 
61
                        }
 
62
                        else if ( !session.isConnected() ) {
 
63
                                throw new LazyInitializationException("could not initialize proxy - the owning Session is disconnected");
 
64
                        }
 
65
                        else {
 
66
                                target = session.immediateLoad(entityName, id);
 
67
                                initialized = true;
 
68
                                checkTargetState();
 
69
                        }
 
70
                }
 
71
                else {
 
72
                        checkTargetState();
 
73
                }
 
74
        }
 
75
 
 
76
        private void checkTargetState() {
 
77
                if ( !unwrap ) {
 
78
                        if ( target == null ) {
 
79
                                getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id );
 
80
                        }
 
81
                }
 
82
        }
 
83
 
 
84
        public final void setSession(SessionImplementor s) throws HibernateException {
 
85
                if (s!=session) {
 
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");
 
89
                        }
 
90
                        else {
 
91
                                session = s;
 
92
                        }
 
93
                }
 
94
        }
 
95
 
 
96
        protected final boolean isConnectedToSession() {
 
97
                return session!=null && 
 
98
                                session.isOpen() && 
 
99
                                session.getPersistenceContext().containsProxy(this);
 
100
        }
 
101
        
 
102
        public final void setImplementation(Object target) {
 
103
                this.target = target;
 
104
                initialized = true;
 
105
        }
 
106
 
 
107
        /**
 
108
         * Return the underlying persistent object, initializing if necessary
 
109
         */
 
110
        public final Object getImplementation() {
 
111
                initialize();
 
112
                return target;
 
113
        }
 
114
 
 
115
        /**
 
116
         * Return the underlying persistent object in the given <tt>Session</tt>, or null,
 
117
         * do not initialize the proxy
 
118
         */
 
119
        public final Object getImplementation(SessionImplementor s) throws HibernateException {
 
120
                final EntityKey entityKey = new EntityKey(
 
121
                                getIdentifier(),
 
122
                                s.getFactory().getEntityPersister( getEntityName() ),
 
123
                                s.getEntityMode()
 
124
                        );
 
125
                return s.getPersistenceContext().getEntity( entityKey );
 
126
        }
 
127
 
 
128
        protected final Object getTarget() {
 
129
                return target;
 
130
        }
 
131
 
 
132
        public boolean isUnwrap() {
 
133
                return unwrap;
 
134
        }
 
135
 
 
136
        public void setUnwrap(boolean unwrap) {
 
137
                this.unwrap = unwrap;
 
138
        }
 
139
 
 
140
}