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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:57:59+00:00 2026-06-07T12:57:59+00:00

I find a lot of posts where it is explained that one should always

  • 0

I find a lot of posts where it is explained that one should always override Equals/GetHashCode on a NHibernate entity class. If I don’t use Sets, is this really necessary?

I simply can’t find a sample where it is shown that missing Equals/GetHashCode can lead to unexpected and wrong behaviour. Everything seems to work perfectly without them. This is really strange that everyone says this is necessary but no one can provide a sample which shows why this is needed .

  • 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-07T12:58:01+00:00Added an answer on June 7, 2026 at 12:58 pm

    There was a question on SO recently about NHibernate doing select N+1 even if fetch is specified. The problem was with missing Equals / GetHashCode implementation.

    The answer links to another similar question.

    Here’s another question on reasoning behind Equals / GetHashCode overrides.

    Nhibernate n+1 with ternary relationship. Want the middle entity in the ternary
    Nhibernate producing proxy despite HQL fetch
    NHibernate: Reasons for overriding Equals and GetHashCode
    Why Equals and GetHashCode are so important to NHibernate
    Why is it important to override GetHashCode when Equals method is overridden?


    Edit

    You don’t need to override them all the time. It may be necessary if you are using composite keys, multiple sessions with detached entities or stateless sessions.

    If you are working with a single session only, NHibernate stores the entities to first level cache using an identity map. Entity comparison in that case is done by comparing ids.

    In cases above (detached entity, stateless session), NHibernate compares actual entities, not their ids. By default, Object.Equals does reference equality. So two objects are equal if they point to the exact same instance. You might have two instances with the same identity, but Object.Equals would return false for them. This is in contrast with the Entity definition:

    An object that is not defined by its attributes, but rather by a
    thread of continuity and its identity.

    JBoss Hibernate wiki has a good explanation on Equals and HashCode with few code examples.

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

Sidebar

Related Questions

On DBPedia you can find a lot of URIs that containing double underscores and
I'm doing something like zgrep somepattern access_log.X.gz But I find that a lot of
I find that if there are a lot of classes the compilation time is
I find myself doing the following a lot, and i don't know if there
Firstly I've looked at a lot of posts on Stackoverflow but I don't see
After a lot of posts in SO and Google, I could not find an
I know that there are a lot of posts about this issue, but I've
I saw a lot of posts here and didn't find an answer. Let me
I have searched a lot of threads but i can't find any answer that
i find a lot of questions about this problem but i did not solve...

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.