KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > EntityEntry


1 //$Id: EntityEntry.java,v 1.15 2005/07/12 20:12:53 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import java.io.Serializable JavaDoc;
5
6
7 import org.hibernate.EntityMode;
8 import org.hibernate.HibernateException;
9 import org.hibernate.LockMode;
10 import org.hibernate.intercept.FieldInterceptor;
11 import org.hibernate.persister.entity.EntityPersister;
12 import org.hibernate.persister.entity.UniqueKeyLoadable;
13 import org.hibernate.pretty.MessageHelper;
14
15 /**
16  * We need an entry to tell us all about the current state
17  * of an object with respect to its persistent state
18  *
19  * @author Gavin King
20  */

21 public final class EntityEntry implements Serializable JavaDoc {
22
23     private LockMode lockMode;
24     private Status status;
25     private final Serializable JavaDoc id;
26     private Object JavaDoc[] loadedState;
27     private Object JavaDoc[] deletedState;
28     private boolean existsInDatabase;
29     private Object JavaDoc version;
30     private transient EntityPersister persister; // for convenience to save some lookups
31
private final EntityMode entityMode;
32     private final String JavaDoc entityName;
33     private boolean isBeingReplicated;
34     private boolean loadedWithLazyPropertiesUnfetched; //NOTE: this is not updated when properties are fetched lazily!
35
private final transient Object JavaDoc rowId;
36
37     EntityEntry(
38             final Status status,
39             final Object JavaDoc[] loadedState,
40             final Object JavaDoc rowId,
41             final Serializable JavaDoc id,
42             final Object JavaDoc version,
43             final LockMode lockMode,
44             final boolean existsInDatabase,
45             final EntityPersister persister,
46             final EntityMode entityMode,
47             final boolean disableVersionIncrement,
48             final boolean lazyPropertiesAreUnfetched) {
49         this.status=status;
50         this.loadedState=loadedState;
51         this.id=id;
52         this.rowId=rowId;
53         this.existsInDatabase=existsInDatabase;
54         this.version=version;
55         this.lockMode=lockMode;
56         this.isBeingReplicated=disableVersionIncrement;
57         this.loadedWithLazyPropertiesUnfetched = lazyPropertiesAreUnfetched;
58         this.persister=persister;
59         this.entityMode = entityMode;
60         this.entityName = persister == null ?
61                 null : persister.getEntityName();
62     }
63
64     public LockMode getLockMode() {
65         return lockMode;
66     }
67
68     public void setLockMode(LockMode lockMode) {
69         this.lockMode = lockMode;
70     }
71
72     public Status getStatus() {
73         return status;
74     }
75
76     public void setStatus(Status status) {
77         if (status==Status.READ_ONLY) {
78             loadedState = null; //memory optimization
79
}
80         this.status = status;
81     }
82
83     public Serializable JavaDoc getId() {
84         return id;
85     }
86
87     public Object JavaDoc[] getLoadedState() {
88         return loadedState;
89     }
90
91     public Object JavaDoc[] getDeletedState() {
92         return deletedState;
93     }
94
95     public void setDeletedState(Object JavaDoc[] deletedState) {
96         this.deletedState = deletedState;
97     }
98
99     public boolean isExistsInDatabase() {
100         return existsInDatabase;
101     }
102
103     public Object JavaDoc getVersion() {
104         return version;
105     }
106
107     public EntityPersister getPersister() {
108         return persister;
109     }
110
111     void afterDeserialize(SessionFactoryImplementor factory) {
112         persister = factory.getEntityPersister( entityName );
113     }
114
115     public String JavaDoc getEntityName() {
116         return entityName;
117     }
118
119     public boolean isBeingReplicated() {
120         return isBeingReplicated;
121     }
122     
123     public Object JavaDoc getRowId() {
124         return rowId;
125     }
126     
127     public void postUpdate(Object JavaDoc entity, Object JavaDoc[] updatedState, Object JavaDoc nextVersion) {
128         this.loadedState = updatedState;
129         
130         setLockMode(LockMode.WRITE);
131         
132         if ( getPersister().isVersioned() ) {
133             this.version = nextVersion;
134             getPersister().setPropertyValue(
135                     entity,
136                     getPersister().getVersionProperty(),
137                     nextVersion,
138                     entityMode
139                 );
140         }
141         
142         FieldInterceptor.clearDirty( entity );
143     }
144
145     public void postDelete() {
146         status = Status.GONE;
147         existsInDatabase = false;
148     }
149     
150     public void postInsert() {
151         existsInDatabase = true;
152     }
153     
154     public boolean isNullifiable(boolean earlyInsert, SessionImplementor session) {
155         return getStatus() == Status.SAVING || (
156                 earlyInsert ?
157                         !isExistsInDatabase() :
158                         session.getPersistenceContext().getNullifiableEntityKeys()
159                             .contains( new EntityKey( getId(), getPersister(), entityMode ) )
160                 );
161     }
162     
163     public Object JavaDoc getLoadedValue(String JavaDoc propertyName) {
164         int propertyIndex = ( (UniqueKeyLoadable) persister ).getPropertyIndex(propertyName);
165         return loadedState[propertyIndex];
166     }
167     
168     
169     public boolean requiresDirtyCheck(Object JavaDoc entity) {
170         
171         boolean isMutableInstance =
172                 status != Status.READ_ONLY &&
173                 persister.isMutable();
174         
175         return isMutableInstance && (
176                 getPersister().hasMutableProperties() ||
177                 !FieldInterceptor.hasInterceptor( entity ) ||
178                 FieldInterceptor.getFieldInterceptor(entity).isDirty()
179             );
180         
181     }
182     
183     public void setReadOnly(boolean readOnly, Object JavaDoc entity) {
184         if (status!=Status.MANAGED && status!=Status.READ_ONLY) {
185             throw new HibernateException("instance was not in a valid state");
186         }
187         if (readOnly) {
188             setStatus(Status.READ_ONLY);
189             loadedState = null;
190         }
191         else {
192             setStatus(Status.MANAGED);
193             loadedState = getPersister().getPropertyValues(entity, entityMode);
194         }
195     }
196     
197     public String JavaDoc toString() {
198         return "EntityEntry" +
199                 MessageHelper.infoString(entityName, id) +
200                 '(' + status + ')';
201     }
202
203     public boolean isLoadedWithLazyPropertiesUnfetched() {
204         return loadedWithLazyPropertiesUnfetched;
205     }
206
207     public void setLoadedWithLazyPropertiesUnfetched(boolean loadedWithAllPropertiesFetched) {
208         this.loadedWithLazyPropertiesUnfetched = loadedWithAllPropertiesFetched;
209     }
210
211 }
212
Popular Tags