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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:24:16+00:00 2026-05-12T05:24:16+00:00

I have a many-to-many relationship defined between two entities using an association table and

  • 0

I have a many-to-many relationship defined between two entities using an association table and cascade=”save-update”.

Entity1 contains a list of Entity2, and conversely, Entity2 contains a list of Entity1. The SQL outputted from this worflow seems ok…

  1. Create an Entity1 and Entity2 object
  2. Add Entity2 to the List on Entity1
  3. Call session.Save on Entity1

-> Insert statements are run for both entities and then a record inserted into the association table linking them together.

However, if I first call session.Save on Entity2, add it to the List, then call session.Save on Entity1 there is an additional UPDATE statement run which sets all of Entity2’s values to exactly the same as what was inserted at the start.

Although not causing any issues, it is an additional query reduce performance. I’ve played with the inverse attribute but this doesn’t eliminate the extra update statement. Currently both sides have inverse=”false” as I want the association table updated no matter which entity is saved.

Any ideas?

  • 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-12T05:24:17+00:00Added an answer on May 12, 2026 at 5:24 am

    Thanks for the response but I believe I’ve found the problem. I had a repository class for each type of entity which implemented the CRUD operations. Each CRUD operation started it’s own session/transaction, called Save/Update/etc then closed the transaction/session.

    If I call the session.Save for Entity2 then session.Save for Entity1 (which contains the list of Entity2) NH seems to know that it has already persisted Entity2 in the session and hence doesn’t try to update the record.

    On the other hand, if I call session.Save for Entity2 then session.Save for Entity1 in a separate session/transaction it wants to update Entity2 again. I’m new to NH so not sure how it tracks which objects require updating, but it must reset between sessions?

    Kinda makes my nice DDD repositories a little less useful though! Maybe the repositories should use a singleton session or something similar to avoid this problem?

    Thanks,
    John

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

Sidebar

Related Questions

Preface: I have two entities defined as a one-to-many relationship: A <<-------> B. B's
I am using nHibernate for db persistence. I have a one-to-many relationship defined between
I have a many-to-many relationship defined in my Symfony (using doctrine) project between Orders
I have defined a many-to-many relationship between my two entity classes User and Permission.
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have a many-to-many relationship between users and groups and I have a table
I'm trying to define a simple one-to-many relationship between two poco's , using the
I have a bi-directional one-to-many relationship defined between Parent and Child classes. I'm looking
I have an intermediate table that defines many-to-many relationship between, for instance, Customer and
I have a many-to-many relationship with a weight factor column in the bridge table

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.