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 3498648
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:30:56+00:00 2026-05-18T12:30:56+00:00

i have x which an object of type objectX which has a property ListOfObjectYs

  • 0

i have x which an object of type objectX which has a property ListOfObjectYs thats a

List<objectY>

the nhibernate mapping looks like this:

    public ObjectXMap()
    {
        HasMany(x => x.ListOfObjectYs).AsBag().Inverse(); 
    }

when i go to save it, i change some properties on objectX and then have:

    Session.SaveOrUpdate(x);

now i need to update this property that is a list. I get a new list of objectYs and i want to replace the existing list of objectY with a new list. do i need to do this?

  foreach (ObjectY y in x.ListOfObjectYs)
                {
                   Session.Delete(y);
                    deleted = true;
                }
                if (deleted)
                {
                   _session.Flush();
                }
                x.ListOfObjectYs.Clear();

                foreach (ObjectY y in newObjectYList)
                {
                    x.ListOfObjectYs.Add(y);
                   Session.SaveOrUpdate(y);
                }
                _session.Flush();

my questions are:

  1. Do i have to delete everything and flush before adding new ones.
  2. Do i need to do all of these incremental saves in between

is there a better way of doing this update where i need to update an object (properties) but also update properties that are list where there is a whole new list (meaning that items need to be deleted and added).

  • 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-18T12:30:57+00:00Added an answer on May 18, 2026 at 12:30 pm

    The answers to your questions are no and no. If you want to replace the list you should clear it and add new items. If you have cascade set to all-delete-orphan, as in James Kovacs’ answer, then the changes to the collection will be persisted when the session is flushed.

    It’s important to understand what Save, Update, and SaveOrUpdate mean in NHibernate:

    • Save – make a new object persistent
    • Update – make a changed detached object persistent
    • SaveOrUpdate – Save or Update depending on the unsaved-value of the object’s identifier

    See also Manipulating Persistent Data.

    Assuming that all your objects were loaded in the same session then replacing a collection may be as easy as:

    x.ListOfObjectYs.Clear();
    foreach (ObjectY y in newObjectYList)
    {
        x.ListOfObjectYs.Add(y);
    }
    _session.Flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have object A which in turn has a property of type Object B
I have a Word.Field object which is a checkbox and the Type property equals
I have a Request object which contains a list of Approvers. An approver has
Lets say I have a single object of type Car which I want to
I have some JS code which generates the following object, return { type: some
I have IQueryable list of objects of type T which I want to transform
So I have an object which has some fields, doesn't really matter what. I
For pupose of explaining let's say that I have a Company object which has
I have a user control - say ControlBase. It has SomeItems property, which is
I have an important Object which have loads of properties. Now few of the

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.