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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:32:55+00:00 2026-05-20T10:32:55+00:00

I have a hash-based collection of objects, such as HashSet or HashMap . What

  • 0

I have a hash-based collection of objects, such as HashSet or HashMap. What issues can I run into when the implementation of hashCode() is such that it can change with time because it’s computed from some mutable fields?

How does it affect Hibernate? Is there any reason why having hashCode() return object’s ID by default is bad? All not-yet-persisted objects have id=0, if that matters.

What is the reasonable implementation of hashCode for Hibernate-mapped entities? Once set the ID is immutable, but it’s not true for the moment of saving an entity to database.

I’m not worried about performance of a HashSet with a dozen entities with key=0. What I care about is whether it’s safe for my application and Hibernate to use ID as hash code, because ID changes as it is generated on persist.

  • 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-20T10:32:56+00:00Added an answer on May 20, 2026 at 10:32 am

    If the hash code of the same object changes over time, the results are basically unpredictable. Hash collections use the hash code to assign objects to buckets — if your hash code suddenly changes, the collection obviously doesn’t know, so it can fail to find an existing object because it hashes to a different bucket now.

    Returning an object’s ID by itself isn’t bad, but if many of them have id=0 as you mentioned, it will reduce the performance of the hash table: all objects with the same hash code go into the same bucket, so your hash table is now no better than a linear list.

    Update: Theoretically, your hash code can change as long as nobody else is aware of it — this implies exactly what @bestsss mentioned in his comment, which is to remove your object from any collections that may be holding it and insert it again once the hash code has changed. In practice, a better alternative is to generate your hash code from the actual content fields of your object rather than relying on the database ID.

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

Sidebar

Related Questions

I'm using a HashSet<T> to store a collection of objects. These objects already have
I have a really weird issue that I can't figure out with comparing objects
I have a twenty byte hex hash that I would like to store in
I have a list of n GUIDs and I need to hash them into
I have a method that creates a MessageDigest (a hash) from a file, and
I have some code on my PHP powered site that creates a random hash
I have an array, and I want to make a hash so I can
I'm trying to have a (hash-based) Multimap with a (hash-based) Multiset of values for
I have several Sets that store the objects of the same class, but I
How can I generate a hash code for an object based on its identity.

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.