0

The title pretty much sums up the issue that I am facing. Basically I have 2 classes:

public class Parent : IIntegerIdentifiable
{
   public virtual long Id { get; set; }
   public virtual ICollection<Child> Children {get; set; }
}

public class Child : IIntegerIdentifiable
{
    public virtual long Id { get; set; }
    public virtual Parent Parent { get; set; }
}

I'm defining a one-to-many relationship between these classes.The class maps for both of them are:

public sealed class ParentMap : ClassMap<Parent>
{
    public ParentMap()
    {
        Id(x => x.Id).GeneratedBy.HiLo(
            "HiLoUniqueKey", "NextHi", "99", "ObjectType = 'Parent'"); 

        HasMany(x => x.Children).KeyColumn("Parent_Id").Inverse().Cascade.AllDeleteOrphan();
    }
}

public sealed class ChildMap : ClassMap<Child>
{
    public ChildMap()
    {
        Id(x => x.Id).GeneratedBy.HiLo("HiLoUniqueKey", "NextHi", "99", "ObjectType = 'Child'");
        References(x => x.Event).Cascade.All();
    }
}

Since I wanted to delete All the children for a particular parent from the database, I wrote the following function in ChildRepository:

    public void ClearChildEntries(long parentId)
    {
        //I had tried the following and was getting errors before I read the other posts on the topic
        //QueryOver().Where(x => x.Event.Id == eventId).List().ToList().ForEach(Remove);
        //Session.flush()

       //Current code
        var parent = _parentRepository.GetById(parentId);
        parent.Children.Clear();

        _parentRepository.SaveOrUpdate(parent);
    }

Our code is setup so that the flush gets called at end of request. Whenever I try executing the code, I get the error:

Cannot insert the value NULL into column 'parent_id', table 'test.dbo.Child'; column does not allow nulls. UPDATE fails.

I also tried first removing each of the child entities and flushing the session and then updating the parent collection. It gives the same error. The question is a) Why is nHibernate trying to update the parent id column to null and then delete? b) What can I do to get the deletes working?

Just so that you know I'm not wasting your time, I have referred other posts on the topic, like
- How to delete child object in NHibernate?
- http://ayende.com/blog/1890/nhibernate-cascades-the-different-between-all-all-delete-orphans-and-save-update
but nothing seems to work. Can someone please point me in the right direction? Thank you for your help!!

Community
  • 1
  • 1
Onkar
  • 159
  • 1
  • 4

1 Answers1

0

I think it has to do something with the Cascade option try changing the Cascade to All_Delete_Orphan From Cascade.All

http://ayende.com/blog/1890/nhibernate-cascades-the-different-between-all-all-delete-orphans-and-save-update

test
  • 36
  • 1
  • Apologies for posting this late. The issue was occurring due to the 'Cascade.All()' in the child map. So when child was removed/deleted, this caused the parent collection's save to get triggered, which gave thhis error. Thanks for your help though!! – Onkar Jul 24 '12 at 07:29