1 package org.hibernate.engine; 3 4 import java.io.Serializable ; 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 21 public final class EntityEntry implements Serializable { 22 23 private LockMode lockMode; 24 private Status status; 25 private final Serializable id; 26 private Object [] loadedState; 27 private Object [] deletedState; 28 private boolean existsInDatabase; 29 private Object version; 30 private transient EntityPersister persister; private final EntityMode entityMode; 32 private final String entityName; 33 private boolean isBeingReplicated; 34 private boolean loadedWithLazyPropertiesUnfetched; private final transient Object rowId; 36 37 EntityEntry( 38 final Status status, 39 final Object [] loadedState, 40 final Object rowId, 41 final Serializable id, 42 final Object 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; } 80 this.status = status; 81 } 82 83 public Serializable getId() { 84 return id; 85 } 86 87 public Object [] getLoadedState() { 88 return loadedState; 89 } 90 91 public Object [] getDeletedState() { 92 return deletedState; 93 } 94 95 public void setDeletedState(Object [] deletedState) { 96 this.deletedState = deletedState; 97 } 98 99 public boolean isExistsInDatabase() { 100 return existsInDatabase; 101 } 102 103 public Object 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 getEntityName() { 116 return entityName; 117 } 118 119 public boolean isBeingReplicated() { 120 return isBeingReplicated; 121 } 122 123 public Object getRowId() { 124 return rowId; 125 } 126 127 public void postUpdate(Object entity, Object [] updatedState, Object 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 getLoadedValue(String propertyName) { 164 int propertyIndex = ( (UniqueKeyLoadable) persister ).getPropertyIndex(propertyName); 165 return loadedState[propertyIndex]; 166 } 167 168 169 public boolean requiresDirtyCheck(Object 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 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 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 |