Description
Abby Doris created an issue — 9th October 2013, 16:34:16:
The object I'm trying to update contains an IList of child objects(Books), mapped something like this:
<bag name="Books" cascade="all-delete-orphan"> <key column="ShelfId" /> <one-to-many class="Book" /> </bag>If i try to save detached entity - only FK will be set to null and no DELETE FROM Book statements generated. Book records still orphaned in database.
Oskar Berggren added a comment — 9th October 2013, 17:09:51:
Please show code sample, preferably in the form of a test case.
Fei added a comment — 4th November 2014, 22:21:34:
Any plan to fix this issue?
Alexander Zaytsev added a comment — 24th November 2014, 14:25:22:
<~idiotsky> any plan to provide a test case?
Fei added a comment — 24th November 2014, 15:17:35:
Sorry, I think this case open a year ago, so you should have a test case. I'll make a test case as soon as possible. thank you for your reply.
Fei added a comment — 25th November 2014, 23:40:47:
here is a example, I don't how to upload the whole project here, so I just put a sample code here, any further information please contact me. thank you.
//ContactTest.cs
using System; using System.Collections.Generic; using System.Linq; using NHibernate; using NHibernate.Cfg; using NHibernate.Linq; using NHibernate.Tool.hbm2ddl; using NUnit.Framework; namespace NH3551 { public class Contact { public Guid Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public IList<Email> Emails { get; set; } } public class Email { public Guid Id { get; set; } public string Address { get; set; } } [TestFixture] public class ContactTest { [Test] public void DeleteTest() { var cfg = new Configuration(); cfg.Configure(); cfg.AddAssembly(typeof(Contact).Assembly); new SchemaExport(cfg).Create(false, true); ISessionFactory sessionFactory = cfg.BuildSessionFactory(); using (ISession session = sessionFactory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { var contact = new Contact { Id = new Guid("ff9a72f9-3731-4927-ad69-a2ddc719623a"), FirstName = "FirstName", LastName = "LastName", Emails = new List<Email> {new Email {Address = "test@email.com"}} }; session.Save(contact); transaction.Commit(); } } using (ISession session = sessionFactory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { var contact = new Contact { Id = new Guid("ff9a72f9-3731-4927-ad69-a2ddc719623a"), FirstName = "FirstName", LastName = "LastName", Emails = new List<Email>() }; // update detached object session.Update(contact); transaction.Commit(); } } List<Email> emails; using (ISession session = sessionFactory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { emails = session.Query<Email>().ToList(); } } // After update I expect the email table is empty. actually this is 1 record in the database. Assert.That(emails, Has.Count.EqualTo(0)); } } }Contact.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false" assembly="NH3551" namespace="NH3551"> <class schema="dbo" lazy="false" name="Contact"> <id name="Id"> <generator class="assigned" /> </id> <property name="FirstName" /> <property name="LastName" /> <list name="Emails" cascade="all-delete-orphan"> <key column="ContactId" /> <list-index column="OrderIndex" /> <one-to-many class="Email" /> </list> </class> </hibernate-mapping>Email.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false" assembly="NH3551" namespace="NH3551"> <class name="Email"> <!-- ids --> <id name="Id"> <generator class="guid.comb" /> </id> <!-- properties --> <property name="Address" not-null="true" /> </class> </hibernate-mapping>