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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:06:24+00:00 2026-05-20T03:06:24+00:00

How can the following problem be dealt with? We’re using lazy loaded NHibernate properties

  • 0

How can the following problem be dealt with?

We’re using lazy loaded NHibernate properties and whenever we’re calling Equals() or GetHashCode() any properties used, will be lazy-loaded, potentially causing a cascade of lazy-loading operations. Eager-loading could be used as an alternative, but I think only in specific cases and not as a general solution.

A typical scenario would look like this:

public class AbstractSaveableObject {
    [Id(0, Name = "Id", UnsavedValue = null)]
    [Generator(1, Class = "native")]
    public virtual long? Id { get; set; }
}

[Class(NameType = typeof(ClassA))]
public class ClassA : AbstractSavableObject {
    [Bag(0, Inverse = true, Cascade = "none")]
    [Key(1, Column = "ClassA")]
    [OneToMany(2, ClassType = typeof(ClassB))]
    public virtual ICollection<ClassB> ClassBs { get; set; }
}

[Class(NameType = typeof(ClassB))]
public class ClassB : AbstractSavableObject {

    [ManyToOne(Column = "ClassA")]
    public virtual ClassA ClassA { get; set; }

    [ManyToOne]
    public virtual ClassC ClassC { get; set; }

    [ManyToOne]
    public virtual ClassD ClassD { get; set; }

    public virtual bool Equals(ClassB other)
    {
        if (ReferenceEquals(null, other))
        {
            return false;
        }
        if (ReferenceEquals(this, other))
        {
            return true;
        }
        return Equals(other.ClassC, ClassC) && Equals(other.ClassD, ClassD);
    }
}

Implementation of GetHashCode and Equals(object) have been omitted for brevity.

What strategies can be been used to tackle this issue?

  • 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-20T03:06:24+00:00Added an answer on May 20, 2026 at 3:06 am

    Two entities are equal if they are of the same type and has the same primary key.

    If you have integers for keys:

    1. Check for reference equality like you do now
    2. If you have the Equal method in some base class you check that the types you’re comparing are equal. Here you can get in to trouble with proxies, I’ll return to that
    3. Check if the primary keys are equal – that will not cause any lazy-loading

    If you have GUIDs for keys:

    1. Check for reference equality like you do now
    2. Check if the primary keys are equal – that will not cause any lazy-loading

    If I have integers for keys I usually have something like this Equal-override in a base class for my entities:

    public virtual bool Equals(EntityBase other)
    {
        if (other == null)
        {
            return false;
        }
    
        if (ReferenceEquals(other, this))
        {
            return true;
        }
    
        var otherType = NHibernateProxyHelper.GetClassWithoutInitializingProxy(other);
        var thisType = NHibernateProxyHelper.GetClassWithoutInitializingProxy(this);
        if (!otherType.Equals(thisType))
        {
            return false;
        }
    
        bool otherIsTransient = Equals(other.Id, 0);
        bool thisIsTransient = Equals(Id, 0);
        if (otherIsTransient || thisIsTransient)
            return false;
    
        return other.Id.Equals(Id);
    }
    

    Now if you entities that inherit from others using table per hierarchy you will face the problem that GetClassWithoutInitializingProxy will return the base class of the hierarchy if it’s a proxy and the more specific type if it’s a loaded entity. In one project I got around that by traversing the hierarchy and thus always comparing the base types – proxy or not.

    In these days though I would always go for using GUIDs as keys and do as described here: http://nhibernate.info/doc/patternsandpractices/identity-field-equality-and-hash-code.html

    Then there is no proxy type mismatch problem.

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

Sidebar

Related Questions

Can I please have a design suggestion for the following problem: I am using
I encountered the following problem. When any bitmap is loaded from resources by an
How can I fix the following problem with IE6/7(IE Tester) with drop-down list(with fixed
Who can help me to fix the following problem? Here is the issue: in
I have the following problem: I have a search box in which user can
I can't for the life of me figure out the following problem (I'm not
I have the following problem: I have an Activity where a user can start
I have the following problem. I am making this website you can see in
Consider following code: My problem is: 1) I can't seem to cast the errors
My problem can be reduced to basically the following set of entities : I

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.