How to get NHibernate ISession to cache entity not retrieved by primary key

My 'user' entity is almost always retrieved by username, rather than by its integer surrogate key. Because the username is not the primary key, this means that the ISession won't cache it and repeatedly hits the database to get the same data.

Is there any way at all I can configure NHibernate to get the ISession to cache users retrieved by username?


It seems the answer is in fact no. Yes, if you're using the second level cache, no if you are using the Session cache.


Yes, you can use NHibernate's natural-id element to accomplish this. See http://ayende.com/Blog/archive/2009/06/23/nhibernate-ltnatural-idgt.aspx, the example is exactly what you're asking for.


The "session cache" is actually not a cache, but an entity map.

My suggestion is that you enable the 2nd level cache for queries using a custom provider that stores items in the HttpContext.


  • NHibernate - Could not execute query - Input string was not in a correct format
  • How do I get NHibernate to save an entity if I assign it an ID, but generate one otherwise?
  • detecting end of http header with \\r\\n\\r\\n
  • Generate a business key
  • SEO friendly 301 redirect .htm to .aspx
  • fluidly dealing with multiple keypresses in ActionScript3
  • Can a Collections.shuffle be considered equivalent to a series of Randoms?
  • Custom WebViewPage inject code when razor template is rendering
  • Configure Spring's MappingJacksonHttpMessageConverter
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • Rest Services conventions
  • SQLite connection strategies
  • Trying to get the char code of ENTER key
  • Elasticsearch script query involving root and nested values
  • Python pickle not one-to-one: different pickles give same object
  • How do I configure context broker accept post requests from my remote sensor?
  • How can I extract results of aggregate queries in slick?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • C++ Partial template specialization - design simplification
  • NHibernate Validation Localization with S#arp Architecture
  • Recording logins for password protected directories
  • Control modification in presentation layer
  • Debugging ASP.NET on a built-in web server suddenly stops
  • What is the “return” in scheme?
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • swift auto completion not working in Xcode6-Beta
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • Cassandra Data Model
  • Redux, normalised entities and lodash merge
  • Android Studio and gradle
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • trying to dynamically update Highchart column chart but series undefined
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • embed rChart in Markdown
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app