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.htmlforastarting hint regarding >> > > 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.-Hidequoted text - >> >> > > - 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 at > http://groups.google.com/group/nhusers?hl=en. > > -- 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.
