/// <summary> Delete a entity. </summary> /// <param name="entityName">The entityName for the entity to be deleted </param> /// <param name="entity">a detached entity instance </param> public void Delete(string entityName, object entity) { using (new SessionIdLoggingContext(SessionId)) { CheckAndUpdateSessionStatus(); IEntityPersister persister = GetEntityPersister(entityName, entity); object id = persister.GetIdentifier(entity); object version = persister.GetVersion(entity); persister.Delete(id, version, entity, this); } }
public override void Execute() { object id = Id; IEntityPersister persister = Persister; ISessionImplementor session = Session; object instance = Instance; bool statsEnabled = Session.Factory.Statistics.IsStatisticsEnabled; Stopwatch stopwatch = null; if (statsEnabled) { stopwatch = Stopwatch.StartNew(); } bool veto = PreDelete(); object tmpVersion = version; if (persister.IsVersionPropertyGenerated) { // we need to grab the version value from the entity, otherwise // we have issues with generated-version entities that may have // multiple actions queued during the same flush tmpVersion = persister.GetVersion(instance, session.EntityMode); } CacheKey ck; if (persister.HasCache) { ck = new CacheKey(id, persister.IdentifierType, persister.RootEntityName, session.EntityMode, session.Factory); sLock = persister.Cache.Lock(ck, version); } else { ck = null; } if (!isCascadeDeleteEnabled && !veto) { persister.Delete(id, tmpVersion, instance, session); } //postDelete: // After actually deleting a row, record the fact that the instance no longer // exists on the database (needed for identity-column key generation), and // remove it from the session cache IPersistenceContext persistenceContext = session.PersistenceContext; EntityEntry entry = persistenceContext.RemoveEntry(instance); if (entry == null) { throw new AssertionFailure("Possible nonthreadsafe access to session"); } entry.PostDelete(); EntityKey key = new EntityKey(entry.Id, entry.Persister, session.EntityMode); persistenceContext.RemoveEntity(key); persistenceContext.RemoveProxy(key); if (persister.HasCache) { persister.Cache.Evict(ck); } PostDelete(); if (statsEnabled && !veto) { stopwatch.Stop(); Session.Factory.StatisticsImplementor.DeleteEntity(Persister.EntityName, stopwatch.Elapsed); } }