your dal uses an NH anti-pattern. the session scope is too short and there is no reason to call session.Flush, since you committed the transaction. you are still thinking in terms of tables and rows. you want to think in terms of objects and workflow/contexts.
a common approach to winforms is session per form. that is each form has it's own session. open the session when the form is activated, dispose the session when the form is closed. as reads/writes are required use a transaction. here is an example http://msdn.microsoft.com/en-us/magazine/ee819139.aspx On Oct 28, 11:35 am, reflection <[email protected]> wrote: > > Would it work in your case to reattach the objects you are about to > > modify in the new ISession, modify them, and then letting NHibernate's > > cascading handle the removal? > > Well at the moment the transaction Logic is complete nested in the > Dal. For every Type of Entity I've got a Dal. Inside the Dal I use one > Session for each method (e.g. FooDal.Save, FooDal.Load and so on). > Typically it looks like this: > > public void Save(Foo foo) > { > using (var session = SessionFactory.OpenSession()) > using (var tx = session.BeginTransaction()) > { > session.Save(foo); > > tx.Commit(); > session.Flush(); > } > > } > > The next problem is that currently my UI is more or less directly > bound to the Foos and if I remove a Bar from Foo it is also removed > from the UI. I could modify it so that the deleted Bars are just > marked as deleted and removed when saving. But you will agree that > that's not what the user is used to. If I delete something it shall > disappear from the UI. If I hit the save Button later the changes go > to the database. If I don't hit the save button the changes are > discarded. The Binding would get pretty tough if would keep the > deleted items in the Foo.Bar Collection till the User pushes the save > button. > > Can't I tell NHibernate that it should remove the whole thing instead > of just the reference to the parent? The easiest way would be to just > set of an SQL command to the database DELETE FROM BAR_TABLE WHERE > ID_BAR = :myDeletedBarId. But I can't imagine that this isn't possible > with NHibernate. > > On Oct 28, 4:58 pm, Oskar Berggren <[email protected]> wrote: > > > 2010/10/28 reflection <[email protected]>: > > > > I tried this, but NHibernate can't track the change because my Entity > > > is detached. I think the problem is that at the moment if I call > > > > session.Delete(bar) > > > > It tries to remove the association between the parent and the child, > > > but it is not allowed to child the foreign key to null because the > > > column has a not-null constraint. But I want to delete the whole > > > record from the database. > > > Would it work in your case to reattach the objects you are about to > > modify in the new ISession, modify them, and then letting NHibernate's > > cascading handle the removal? > > > > Shall I refactor my Dal to use a Singleton for the Session? Then the > > > entities would never get detached... > > > You most certainly shall not. :) But you said session-per-request > > previously... what is your definition of request in your winforms app? > > > /Oskar > > > > This is a Windows Forms Application. > > > > On Oct 28, 3:23 pm, Jason Meckley <[email protected]> wrote: > > >> inverse=true and cascade=all-delete-orphan > > > >> var bar = session.Load<Bar>(1); > > >> session.Get<Foo>(2).Bars.Remove(bar); > > > >> by removing bar from the collection of bars within Foo it becomes > > >> orphaned. nh detects the orphan and deletes it. > > >> by setting bar.Foo = null you are attempting to update the mapped > > >> column to null, which causes a foreign key reference error. > > > >> On Oct 28, 8:24 am, reflection <[email protected]> wrote: > > > >> > I think I can't get the full picture. Can you tell me how this helps > > >> > in my situation? I understand what it means, but I can't see how this > > >> > can be helpfull too me -.- > > > >> > Thanks a lot!!! > > > >> > On Oct 28, 1:34 pm, Oskar Berggren <[email protected]> wrote: > > > >> > > See the last paragraph of 6.4 > > >> > > inhttp://nhforge.org/doc/nh/en/index.htmlforastartinghintregarding > > >> > > inverse. > > > >> > > /Oskar > > > >> > > 2010/10/28 reflection <[email protected]>: > > > >> > > > Hello, > > > >> > > > I've read a lot on the Internet but I can't find a solution for my > > >> > > > problem :( > > > >> > > > Example: > > > >> > > > public class Foo > > >> > > > { > > >> > > > public IList<Bar> Bars { get; set; } > > >> > > > } > > > >> > > > public class Bar > > >> > > > { > > >> > > > public Foo Foo { get; set; } > > >> > > > } > > > >> > > > So it's a classical many-to-one mapping. > > > >> > > > The mapping: > > > >> > > > Foo: > > > >> > > > ... > > >> > > > <bag name="Bars" inverse="true" cascade="none" lazy="true"> > > >> > > > <key column="ID_FOO" /> > > >> > > > <one-to-many class="Bar"/> > > >> > > > </bag> > > >> > > > ... > > > >> > > > Bar: > > > >> > > > ... > > >> > > > <many-to-one fetch="select" index="IDX_BAR_FOO" name="Foo" > > >> > > > class="Foo" > > >> > > > column="ID_FOO" cascade="none" not-null="true" foreign- > > >> > > > key="FK_BAR_FOO" /> > > >> > > > ... > > > >> > > > I use Session per Request pattern. So I do only have detached > > >> > > > entities > > >> > > > in my code. > > > >> > > > Now if I load a Foo with some Bars from the Database, the tree may > > >> > > > look like this: > > > >> > > > Foo1 > > >> > > > |->Bar1 > > >> > > > |->Bar2 > > >> > > > |->Bar3 > > > >> > > > Now I remove one Bar from Foo1: > > > >> > > > Foo1.Bars.Remoe(Bar1); > > >> > > > Bar1.Foo = null; > > > >> > > > The problem is that I can't do > > > >> > > > session.Delete(Bar1); > > > >> > > > It always tells me that Foo can't be NULL. That's OK. But how do I > > >> > > > tell NHibernate to delete the whole row? If the entities would not > > >> > > > be > > >> > > > detached I could use delete-orphan on the collection, but since the > > >> > > > childs are detached this doesn't work neither :( > > > >> > > > I hope anybody has an idea how to solve this. > > > >> > > > Thanks in advance!!!! > > > >> > > > Greetings > > > >> > > > -- > > >> > > > You received this message because you are subscribed to the Google > > >> > > > Groups "nhusers" group. > > >> > > > To post to this group, send email to [email protected]. > > >> > > > To unsubscribe from this group, send email to > > >> > > > [email protected]. > > >> > > > For more options, visit this group > > >> > > > athttp://groups.google.com/group/nhusers?hl=en.-Hidequotedtext- > > > >> > > - Show quoted text -- Hide quoted text - > > > >> - Show quoted text - > > > > -- > > > You received this message because you are subscribed to the Google Groups > > > "nhusers" group. > > > To post to this group, send email to [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]. > > > For more options, visit this group > > > athttp://groups.google.com/group/nhusers?hl=en.-Hide quoted text - > > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
