Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 627995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:35:19+00:00 2026-05-13T19:35:19+00:00

I have parent child relationship between two entities(Parent and Child). My Parent mapping is

  • 0

I have parent child relationship between two entities(Parent and Child).

My Parent mapping is as follows:

<class name="Parent" table="Parents">
    ...
    <bag name="Children" cascade="all">
        <key column="ParentID"></key>
        <one-to-many class="Child"></one-to-many>
    </bag>
</class>

I would like to execute the following:

someParent.Children.Remove(someChild);

The Child class has a reference to another parent class, Type. The relationship looks like

relation diagram

Note: I apologize for the non-linked url above, I couldn’t seem to get past the asterisk in the url string using Markup

Due to this relationship, when the above code is called, instead of a DELETE query, an UPDATE query is generated which removes the ParentID from the Child table(sets to null).

Is it possible to force NHibernate to delete the child record completely, when removed from the Parent.Children collection?

UPDATE

@Spencer’s Solution

Very attractive solution as this is something that can be implemented in future classes. However, due to the way sessions are handled(in my particular case) in the repository pattern, this is near impossible as we would have to pass session types(CallSessionContext/WebSessionContext) depending on the application.

@Jamie’s Solution

Simple and quick to implement, however I’ve hit another road block. My child entity looks as follows:
entity object

When using the new method, NHibernate generates an update statement setting the TypeID and ParentID to null, as opposed to a single delete outright. If I missed something within the implementation, let me know as this method would be painless to move forward with.

@The One-Shot-Delete solution described here, outlines an idea dereferencing the collection to force a single delete. Same results as above however, an update statement is issued.

//Instantiate new collection and add persisted items
List<Child> children = new List<Child>();
children.AddRange(parent.Children);

//Find and remove requested items from new collection
var childrenToRemove = children
    .Where(c => c.Type.TypeID == 1)
    .ToList();

foreach (var c in childrenToRemove) { children.Remove(m); }
parent.Children = null;

//Set persisted collection to new list
parent.Children = Children;

Solution

Took a bit of digging, but Jamie’s solution came through with some additional modifications. For future readers, based on my class model above:

Type mapping – Inverse = true, Cascade = all

Parent mapping – Inverse = true, Cascade = all-delete-orphan

Remove methods as described in Jamie’s solution works. This does produce a single delete statement per orphaned item, so there is the possibility for tuning in the future, however the end result is successful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:35:20+00:00Added an answer on May 13, 2026 at 7:35 pm

    Instead of exposing the IList<Child>, control access to the collection through a method:

    RemoveChild(Child child)
    {
        Children.Remove(child);
        child.Parent = null;
        child.Type.RemoveChild(child);
    }
    

    Type.RemoveChild would look similar but you would have to be careful to not put it into an infinite loop calling each other’s RemoveChild methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;
I have a parent-child relationship between two objects. Parent :has_many :children Child :belongs_to :parent
I have a parent child relationship between 2 classes Parent @Entity @Table(name = PARENT)
I have a one to many entity relationship between two entities: EntityP (Parent) <-->>
I have an identifying relationship between two tables in the database. The parent table
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have this situation where I have a parent child relationship between two sets
I have two tables with a join table between them: Parent: Pages Child: Things
I have a database table with parent child relationships between different rows. 1 parent
Using parent child relationship where a parent can have children while each child has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.