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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:10:51+00:00 2026-05-15T10:10:51+00:00

I have an object where I maintain a relationship to another object: public class

  • 0

I have an object where I maintain a relationship to another object:

public class Foo {
    public Bar Bar { get; set; } 
}

In the mapping I reference Bar to that I can maintain the relationship – I don’t want the parent object to update properties in the child, just the existence of the relationship:

References(x => x.Bar, "BarId")
  .Cascade.None();

In the UI layer I create the relationship using a property which is not the underlying primary key:

item.Bar = new Bar { Code = "123" };

In the repository layer I hydrate the object if it doesn’t have the primary key populated:

if(item.Bar.Id == null)
{
  item.Bar = barRepository.RetrieveByCode(item.Bar.Code);
}

When I the RetrieveByCode line runs (which is a Criteria.UniqueResult under the covers) I get a TransientObjectException telling me that “the object references an unsaved transient instance – save the transient instance before flushing” for the Bar type.

When I run the same code path without creating the temporary Bar object it works. It appears that the Bar created as a temporary oject is tracked by NHibernate, yet I want it to forget that it ever existed as it is only a placeholder.

Any thoughts on how to achieve this?

UPDATE: Doing some more testing on this it seems to be the change tracking in Foo that is causing trouble. If I call Session.Evict(item) after retrieving it, but before making any changes and then re-attach the object using Session.Update(item) after I am done it seems to work, however it updates the child objects which is not what I want – I only want to manage the relationship.

UPDATE 2: I changed the FlushMode from Auto to Commit. It seems to have disabled the queueing of any interim changes to the object. Having researched NH behavior a bit further it seems that Update works more like a “re-attach” call rather than an explicit “update now” call.

UPDATE 3: It appears changing FlushMode caused other issues with transactions that required several operational steps. I reverted back to try another approach:

if(item.Bar.Id == null) 
{ 
  var barCode = item.Bar.Code;
  item.Bar = null;
  item.Bar = barRepository.RetrieveByCode(barCode); 
}
  • 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-15T10:10:52+00:00Added an answer on May 15, 2026 at 10:10 am

    Why do you want it to work that way? Why not simply set item.Bar using the retrieved Bar object:

    item.Bar = barRepository.RetrieveByCode("123");
    

    You might be able to make your current pattern work using Load:

    if(item.Bar.Id == null)
    {
        var bar = barRepository.RetrieveByCode(item.Bar.Code);
        item.Bar = session.Load<Bar>(bar.Id);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object in a multi-threaded environment that maintains a collection of information,
I have an object that is mapped to a cookie as a serialized base-64
I have an object of class F. I want to output the contents of
I have an object that implements IDisposable that is registered with the Windsor Container
I have an object that needs a test if the object data is valid.
I have an object, that is facing a particular direction with (for instance) a
I have object A which in turn has a property of type Object B
I have an object graph serialized to xaml. A rough sample of what it
I have an object of the type System.Drawing.Image and want to make every pixel
I have an object instance which I access with the ME as it accesses

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.