Michael, I think the question should really be: why are you modelling thing that way? Couldn't you model it in another way?
In one-to-many or many-to-one relationship, the "one" part is the parent and the "many" part is the children. What you're asking is actually if you deleted the child, you wanted the parent to cascadedly deleted. And that is just making any sense to me, as Jordan pointed out. Looking at your code, I'm not sure which one the parent is and which one child is. So now, I'm asking you: could you tell me which one the parent is and which one the child is? Or are they actually the same entity splitted in multiple table, which implies one-to-one relationship as Ramon pointed out before? On Wed, May 2, 2012 at 4:53 PM, Michael Charalambous < [email protected]> wrote: > Hi Jordan > > Thank you for you response, however I would also really like to know the > logic behind not supporting this functionality and what is the recommended > work-around for it. > > I have quite a bit of googling on this issue and haven't found any > information on why NHibernate behaves this way, just that it does. I really > like to understand why things work a particular way so I really want an > explanation of why it behaves this way. > > Thanks > Michael > > > > On Tuesday, 1 May 2012 17:44:38 UTC+1, Jordan wrote: >> >> Sorry Michael, my bad, I didn't read your post properly at all. >> >> I believe, as you suggest, that this behaviour only works when the >> parent object is deleted, hence the all-delete-orphan option is only >> available that way round. >> >> regards, >> Jordan >> >> >> On May 1, 5:00 pm, Michael Charalambous >> <michael.charalamb...@gmail.**com> wrote: >> > Hi Jordan >> > >> > Thank you for you response. I have tried this and it does not fix the >> > issue. From the docs the only valid cascade options for many-to-one are >> > >> > cascade="all|none|save-update|**delete" >> > >> > Thanks >> > >> > Michael >> > >> > >> > >> > >> > >> > >> > >> > On Tuesday, 1 May 2012 16:32:04 UTC+1, Jordan wrote: >> > >> > > Hi Michael, >> > >> > > I believe you need to do: >> > >> > > <many-to-one name ="sequence" cascade="all-delete-orphan" /> >> > >> > > regards, >> > > Jordan. >> > >> > > On May 1, 3:05 pm, Michael Charalambous >> > > <michael.charalamb...@gmail.**com> wrote: >> > > > Hi >> > > > I am confused about how NHibernate treats orphan objects and would >> like >> > > > some clarification on this subject. Please see below for my simple >> test >> > > > case. When I run this test case I expect the child object >> "sequence" to >> > > be >> > > > deleted from the DB but this isn't the case, instead I am left with >> an >> > > > orphan object. The documentation on many-to-one, >> > >http://www.nhforge.org/doc/**nh/en/index.html#mapping-** >> declaration-manyt..<http://www.nhforge.org/doc/nh/en/index.html#mapping-declaration-manyt..>., >> >> > > > states that the cascade option: >> > >> > > > "Specifies which operations should be cascaded from the parent >> > > object >> > > > to the associated object" >> > >> > > > To be honest, I'm not sure if this means that my delete scenario is >> > > > supported or if the cascade option only comes into play when the >> parent >> > > > object is deleted. >> > >> > > > So to summarise my questions are: >> > >> > > > 1. Does NHibernate support many-to-one deletes as in the unit >> test, >> > > and >> > > > if so what is wrong with my test case. >> > > > 2. If NHibernate does not support this scenario what is the >> logic >> > > behind >> > > > not supporting it. >> > > > 3. If NHibernate does not support this scenario is there a work >> > > around >> > > > so child objects can be deleted by their parents from within the >> > > domain >> > > > model and be deleted within the DB. >> > >> > > > Thanks in advance >> > >> > > > Michael >> > >> > > > NB. I have also tried using one-to-one and described by Ayende ( >> > >http://ayende.com/blog/3960/**nhibernate-mapping-one-to-one<http://ayende.com/blog/3960/nhibernate-mapping-one-to-one>) >> with the same >> > > > results. >> > >> > > > ------------------------------**--Class >> > > > declarations------------------**--------------------------- >> > >> > > > public class Host >> > > > { >> > > > public virtual int id { get; set; } >> > > > public virtual Sequence sequence { get; set; } >> > >> > > > } >> > >> > > > public class Sequence >> > > > { >> > > > public virtual int id { get; set; } >> > >> > > > } >> > >> > > > ------------------------------**--XML >> > > > declarations------------------**--------------------------- >> > >> > > > <class name="Host"> >> > > > <id name="id"> >> > > > <generator class="native" /> >> > > > </id> >> > > > <many-to-one name ="sequence" cascade="all" /> >> > > > </class> >> > >> > > > <class name="Sequence"> >> > > > <id name="id"> >> > > > <generator class="native" /> >> > > > </id> >> > > > </class> >> > >> > > > ------------------------------**--Unit >> > > > Test--------------------------**------------------- >> > >> > > > [TestFixture] >> > > > public class Fixture : BugTestCase >> > > > { >> > > > [Test] >> > > > public void CanDeleteChild() >> > > > { >> > > > Host host = new Host(); >> > > > Sequence sequence = new Sequence(); ; >> > > > host.sequence = sequence; >> > >> > > > using (var s = OpenSession()) >> > > > { >> > > > using (s.BeginTransaction()) >> > > > { >> > > > s.SaveOrUpdate(host); >> > > > s.Transaction.Commit(); >> > > > } >> > > > s.Close(); >> > > > } >> > >> > > > int sequence_id = sequence.id; >> > >> > > > using (var s = OpenSession()) >> > > > { >> > > > using (s.BeginTransaction()) >> > > > { >> > > > sequence.host = null; >> > > > sequence = null; >> > > > host.sequence = null; >> > >> > > > s.SaveOrUpdate(host); >> > > > s.Transaction.Commit(); >> > > > } >> > > > s.Close(); >> > > > } >> > >> > > > using (var s = OpenSession()) >> > > > { >> > > > Assert.IsNull(s.Get<Sequence>(**sequence_id)); >> > > > sessions.Close(); >> > > > } >> > > > } >> > >> > > > } > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/nhusers/-/72iffKkJ9_MJ. > > 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. > -- Regards, Maximilian Haru Raditya -- 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.
