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

  • Home
  • SEARCH
  • 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 3335222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:59:36+00:00 2026-05-17T23:59:36+00:00

What’s the proper way to reference entities which were already persisted in a different

  • 0

What’s the proper way to reference entities which were already persisted in a different session, but remained unchanged in memory after a session has been closed?

Once NH persists an entity in a single session and that session is closed, I need to fetch it again in a new session in order to reference it in some other transient entities. Or, I can call Session.Lock on the new session instance and let NH know this entity is not transient. This item should never change during app lifetime.

What would be the preferred way to use? Is there some other way to do it?

[Edit]

Additional problem is that I am using the Repository pattern, and not using NH directly in my business layer. This means that I also need to have a way to abstract this call to Session.Lock, i.e. have a method similar to Session.Lock in my repository interface. This is a bit smelly IMHO.

  • 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-17T23:59:37+00:00Added an answer on May 17, 2026 at 11:59 pm

    Concerning the first question. If you are just going to read the entity, you don’t have to do anything. Even assigning it as a field to a different entity will not require you to lock the record. The only reason you need to call ISession.Lock is when you want to mutate and then save the entity.

    There is an exception and that is lazy loading. If the entity has foreign child records that were not loaded while the first session was active, an exception will be thrown when you try to access them later on. The easiest way to get around is, is to touch the child collections in first session.

    If the entity is still giving you problems under these circumstances, you can add a Load to your repository. You can wire this to ISession.Load. What Load does is create an empty proxy for the entity without hitting the database. This entity is a part of the session the entity is loaded with and can be used to assign to properties of other entities. The advantage of this approach is that it’s a lot cleaner and it’s easy to mock with unit testing.

    Concerning the second question. Yes, you’re right that it smells to integrate ISession.Lock into a repository. Again, when you don’t have to mutate the entity, you don’t have to worry about this. But, when you are, well, you really should think about just re-loading the entity from the repository and work on that one. I know it’s not as optimal as could be, but it saves you a lot of very strange code, specifically in your unit tests.

    One last thing. I understand that you’re talking about an entity that will live for a long time (maybe the full runtime of the application). You have roughly three categories of lifetime: 1. forever, 2. long and 3. short. The reason I mention this is that more than once, problems with entities that have a ‘long’ lifetime could really just stay connected to a session that has the same lifetime. It’s not a problem to have a session alive for say, 5 or 10 minutes (the time the user is entering data in a form). This alone will save most people a lot of trouble.

    One more note: have a look at NHibernateUtil and NHibernateProxyHelper. These classes can help you force loading of entities and child collections.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.