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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:32:12+00:00 2026-05-11T14:32:12+00:00

I have a parent object which has a one-to-many relationship with an ISet of

  • 0

I have a parent object which has a one-to-many relationship with an ISet of child objects. The child objects have a unique constraint (PageNum and ContentID – the foreign key to the parent).

<set name='Pages' inverse='true' cascade='all-delete-orphan' access='field.camelcase-underscore'>     <key column='ContentId' />     <one-to-many class='DeveloperFusion.Domain.Entities.ContentPage, DeveloperFusion.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' /> </set> 

The problem I’ve hit is if I remove a ContentPage element from the parent collection, and then add a new one with the same unique key within the same transaction… You get a unique constraint violation because NHibernate attempts to perform the insert before the delete.

Is there a way to force NHibernate to perform the delete first?

  • 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. 2026-05-11T14:32:13+00:00Added an answer on May 11, 2026 at 2:32 pm

    There is no option to specify the order of operations in a transaction as it is hard-coded as follows (from the documentation):

    The SQL statements are issued in the following order

    • all entity insertions, in the same order the corresponding objects were saved using ISession.Save()
    • all entity updates
    • all collection deletions
    • all collection element deletions, updates and insertions
    • all collection insertions
    • all entity deletions, in the same order the corresponding objects were deleted using ISession.Delete()

    (An exception is that objects using native ID generation are inserted when they are saved.)

    As such, can I challenge you to answer why you are adding a new entity with an existing identifier? An identifier is supposed to be unique to a specific "entity." If that entity is gone, so should be its identifier.

    Another option would be to do an update on that record instead of a delete/insert. This keeps the ID the same so there is no unique constraint violation (on the key at least) and you can change all the other data so that it’s a "new" record.

    EDIT: So apparently I wasn’t entirely paying attention to the question when I responded since this is a problem with a unique constraint on a non-primary-key column.

    I think you have two solutions to choose from:

    1. Call Session.Flush() after your delete which will execute all the changes to the session up to that point, after which you can continue with the rest (inserting your new object). This works inside of a transaction as well so you don’t need to worry about atomicity.
    2. Create a ReplacePage function which updates the existing entity with new data but keeps the primary-key and the unique column the same.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent object which has a one to many relationship with an
I have two Entity Framework objects with a one-to-many relationship: widget(parent) and widgetNail(child) Widget
I have a parent object Compound with a one-to-many relationship to an object Submission.
i have two entities named Parent and Child , linked in a one-to-many relationship.
In a situation where you have a parent class which has one child class,
I have a parent class which contains a child object. I am using set
I have this huge domain object(say parent) which contains other domain objects. It takes
I have a parent object called Page that has a List of objects called
I have a parent object, Post, which has the following children. has_one :link has_one
I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;

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.