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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:36:22+00:00 2026-06-14T02:36:22+00:00

As per the title, I have an object which has a ReferencesAny association, but

  • 0

As per the title, I have an object which has a ReferencesAny association, but it’s possible that the referenced object may have been deleted. If this has occurred, currently the missing object is initialized with a proxy which then throws ObjectNotFound if accessed. I want to be able to have this return null instead, in a similar fashion to setting not-found to “ignore” on other properties. I understand the differences between using Session.Get and Session.Load but this is occurring as a result of a LINQ query.

So, my question is, how can I specify that null should be returned for missing objects for an “any” association?

  • 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-14T02:36:24+00:00Added an answer on June 14, 2026 at 2:36 am

    I am using xml configuration, but I guess that my hint could help you anyway… NHibernate provides for those incomplete mappings an attribute not-found which is by default set to “exception“. In this example is reference many-to-one mapping, but it could be used even for one-to-many

    <many-to-one name="MyReferencedObject" not-found="exception" />
    

    but it can be changed to ignore

    <many-to-one name="MyReferencedObject" not-found="ignore" />
    

    In that case the result will be NULL. (But be careful, NHibernate will always execute a SQL select statement, because such a “NULL” value won’t be cached)

    EDIT: based on the comment below, I have to say, that my suggestion is not working for <any> mapping. Sorry for that, I should read more carefully…
    But, please, let me provide you with suggestion, how to fix it.

    We can introduce event listener. More can be found here http://nhibernate.info/doc/nh/en/index.html#objectstate-events

    Solution in a nutshell:

    Introduce the event listener

    public class PostLoadListener 
               : NHibernate.Event.Default.DefaultPostLoadEventListener
    { ... }
    

    and inject it into the “session-factory” configuration.

    <event type="post-load">
        <listener class="MyLib.PostLoadListener, MyLib"/>
    </event>
    

    The trick would be inside of the overriden method OnPostLoad

    public override void OnPostLoad(PostLoadEvent @event)
    {
        base.OnPostLoad(@event);
    
        // the entity with <any> mapping 
        ConvertToNull(@event.Entity as MyAuditEntity); 
    }
    
    protected virtual void ConvertToNull(MyAuditEntity item)
    {
        if (item == null)
        {
            return;
        }
        try
        {
            // access some property to check that reference is not a PROXY
            var id = item.AnyEntity.ID;
        }
        catch
        {
            // replace proxy with null
            item.AnyEntity = null; 
        }
    }
    

    Of course, this is not the answer like not-found=”ignore”. But this workaround will do that job.

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

Sidebar

Related Questions

As per the title I have three parts to this question... Is db4o object
As per title. I have a piece of jQuery that looks like the following
This question has been asked before: AlertDialog custom title has black border But was
As per the title, is this possible? By tri-state, I mean the parent node
I have an object submission which contains some informations which I would like to
Exactly as per the title. Simply in my HomeController I have: string Username =
I have an object structure that is mimicking the properties of an excel table.
As per the title, I have created a custom control. On a certain button
The title is a bit complicated, but it fits my problem. I have a
1: I have a .html file that has some markup with some placeholder tags.

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.