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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:31:44+00:00 2026-05-12T19:31:44+00:00

i have read in more then one place that using NHibernate’s Identifier as Primary

  • 0

i have read in more then one place that using NHibernate’s Identifier as
Primary Key is considered a bad practice because the id are generated at the server side
and hence we need a reply from the server about the generated ids.
(i think that i have also seen a post by Ayende that says that ms-sql server may have problems
generating identifier and this can cause primary key violations)

now, let’s say i have a very simple domain-model: blogs and post. where every blog can have
many posts and each post belongs to EXACTLY one blog (one to many relationship)
but WE ONLY SAVE THE Post -> Blog relationship and not the Blog -> Posts

now suppose i use NHibernate WITH Idenitifer Id Generation such that i don’t use cascading so i have in my c# code something like this:


SaveBlogInTransaction(blog1);

SavePostsInTransaction(blog1posts);

now, my question is this: if the insertion can be done only in one place in my code
(there are no concurrency issues), is it guaranteed that the posts’ connection to their blog in the db will be valid?

i mean let’s look at the Posts’ table schema:
PostId, PostName, PostType, BlogId

is it guaranteed that the BlogId will be valid?

note: each method works as a transaction and at the end it does a commit to the db

  • 1 1 Answer
  • 1 View
  • 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-12T19:31:45+00:00Added an answer on May 12, 2026 at 7:31 pm

    The posts will have the correct Blog ID even in your re-phrased question. NHibernate will insert the Blog row in the database as soon as Save is called and will retrieve the generated identity value using SCOPE_IDENTITY at that time. This ID value will be stored in the Blog object and be available for any posts created later.

    This is why people say that identity-based IDs are not a good choice – NHibernate must execute the SQL to insert the row immediately so that the correct identity value can be obtained. If a different identity generator is used NHibernate can avoid executing the SQL insert statements until the transaction is committed since the database does not need to be contacted to determine the value of the object’s ID.

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

Sidebar

Related Questions

I have read that most languages are becoming more and more like lisp, adopting
I have a couple of identical files stored in more than one place on
i have the following code: <p class=more><a href=/stories class=more>Read more</a></p> and CSS: a.more, a.back
In wordpress, I have the loop.php file. There I can see the 'read more'
I have read that you can do it, but would this really improve performance
I have read multiple articles about why singletons are bad. I know it has
I have read in a lot of places that assembly language is not usually
I have read many article about this one. I want to hear from you.
I have a very large array of doubles that I am using a disk-based
In my place where I work, used to have files with more than million

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.