Hi, I think your problem emerges from misunderstanding the way NHibernate deals with the cascades. If we refer to the second case - it works because NHibernate can manage persistent objects. If you delete root, then NHibernate reads the mappings and decide if it should delete the associated collections - and in this case it is true. So when you read your Receipe then NH automagically deletes Temperatures and LampControls associated with the just read Receipe entity as well. And this is expected behavior. In the first case you are running explicit query on the database through NHibernate. And now think it over - how the NH should know what you are trying to do in your query? How NH should know what your expected behavior should be like? As a solution I see two options here. First, if you would like use explicit query on the database I would use dedicated query: delete from Temperatures delete from LampControls delete from Receipe And the second option is defining cascading action (ie. http://www.techrepublic.com/blog/the-enterprise-cloud/defining-cascading-referential-integrity-constraints-in-sql-server/) or usage the database trigger.
regards, Kamil W dniu środa, 18 grudnia 2013 12:22:43 UTC+1 użytkownik [email protected] napisał: > > Help please. > Have this > > public class RecipsMap : ClassMap<Data.Database.Recipe> > { > public RecipsMap() > { > this.Table("Recipe"); > this.Id(x => > x.Id).GeneratedBy.Native().Column("Id").UnsavedValue(0); > this.Map(x => x.Name).Not.Nullable().Length(50).Unique(); > this.Map(x => x.Description).Nullable(); > this.Map(x => x.Span); > this.HasMany<Data.Database.Temperature>(x => > x.Temperatures).Table("Temperature").Inverse().Cascade.AllDeleteOrphan(); > this.HasMany<Data.Database.LampControl>(x => > x.LampControls).Table("LampControl").Inverse().Cascade.AllDeleteOrphan(); > } > } > > > > and in repository > > /** Удалить все */ > public void Delete() > { > var metadata = > this._sessionFactory.GetClassMetadata(typeof(T)) as > NHibernate.Persister.Entity.AbstractEntityPersister; > string table = metadata.TableName; > > using (var session = this._sessionFactory.OpenSession()) > { > using (var transaction = session.BeginTransaction()) > { > string deleteAll = string.Format("DELETE FROM > \"{0}\"", table); > session.CreateQuery(deleteAll).ExecuteUpdate(); > transaction.Commit(); > } > } > > /** сообщим коллбэку что данные изменены */ > this.OnOnDataChange(this.Get()); > } > > > /** Удалить по имени */ > public void Delete(string name) > { > using (var session = this._sessionFactory.OpenSession()) > { > using (var transaction = session.BeginTransaction()) > { > T recipe = > session.QueryOver<T>().Where(Restrictions.Eq("Name", name)).SingleOrDefault(); > session.Delete(recipe); > transaction.Commit(); > } > } > > /** сообщим коллбэку что данные изменены */ > this.OnOnDataChange(this.Get()); > } > > > trouble is that data are not deleting cascadely in first case "delete > all", but everything is deleting while deleting separate object in second > case. If selecting by name as here - all right, if delete all - only recipe > table is removing and all other (temperatures and LampControl) stay at > there places. Who dealed with this plz give me advice - hoe to remove all > data from table? > thnks > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/nhusers. For more options, visit https://groups.google.com/groups/opt_out.
