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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:37:36+00:00 2026-06-01T08:37:36+00:00

How would I use nHibernate,configured by fluent nhibernate if it makes any difference, to

  • 0

How would I use nHibernate,configured by fluent nhibernate if it makes any difference, to load an entity using natural/alternate key in some cases, rather than the primary key when using the Load method on an ISession.

I still need the functionality to allow me to do both, and in the majority of cases, the entity will be loaded via the PKey, but in some cases (where an external system is involved), I need to select the record using the natural key.

I’d like to keep the performance benefit Load allows, rather than do a query etc.

 // Current
 int countryID = 1; // from normal input source

 Address a = new Address();
 a.Country = session.Load<Country>(countryID);

 session.SaveOrUpdate(a);

 // Required
 string countryCode = "usa"; // from external input source

 Address a2 = new Address();
 a2.Country = session.LoadViaNatualKeySomehow<Country>(c=> c.Code, countryCode); // :)

 session.SaveOrUpdate(a2);
  • 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-06-01T08:37:37+00:00Added an answer on June 1, 2026 at 8:37 am

    AFAIK, it is not possible. As you can see in Ayendes post, there is a query syntax for criteria, the only natural ID in the whole NHibernate API as far as I know. This query translates into a “normal” query, except of the second level cache handling as described in this post.

    It would be nice if it wouldn’t at least flush the session.

    one simple performance enhancement you can do is turning off auto flush before querying by the (immutable!) natural ID:

    session.FlushMode = FlushMode.Never;
    session.CreateQuery(...by natural id ...);
    session.FlushMode = FlushMode.Auto;
    

    This can make a big difference, but does of course not compete to Load.

    The reason why it doesn’t exist is most probably the fact the entities in the session are all identified by the id.

    If you had it:

    var entity1 = session.Load<Entit>(id);
    // does not exist
    var entity2 = session.LoadByNaturalKey(natural id);
    

    How could NH determine that the id and the natural id are identifying the same object, without loading them from the database? The whole session cache gets into trouble.

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

Sidebar

Related Questions

I am use nhibernate with fluent nhibernate. I wondering if there is any difference
I thought I would use a stored routine to clean up some of my
I have a c# library project, that i configured using nhibernate, and I like
i am using Spring.net 1.2 with NHibernate 2.0.1. Within my project i'am facing some
I am using NHibernate 3.1 and Fluent NHibernate as ORM in my project. I
I currently have Spring configured to use HSQL, but I would like to use
I am using Spring MVC for my project and would like to use Apache
I would like to use NHibernate Automapper to map the following class: public class
In the current application I'm developing I'm using Fluent NHibernate to configure NHibernate for
Instinctively, I would say that this is impossible, as NHibernate needs to know some

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.