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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:05:53+00:00 2026-05-14T18:05:53+00:00

How do you handle object equality for java objects managed by hibernate? In the

  • 0

How do you handle object equality for java objects managed by hibernate? In the ‘hibernate in action’ book they say that one should favor business keys over surrogate keys.
Most of the time, i do not have a business key. Think of addresses mapped to a person. The addresses are keeped in a Set and displayed in a Wicket RefreshingView (with a ReuseIfEquals strategy).

I could either use the surrogate id or use all fields in the equals() and hashCode() functions.
The problem is that those fields change during the lifetime ob the object. Either because the user entered some data or the id changes due to JPA merge() being called inside the OSIV (Open Session in View) filter.

My understanding of the equals() and hashCode() contract is that those should not change during the lifetime of an object.

What i have tried so far:

  • equals() based on hashCode() which uses the database id (or super.hashCode() if id is null). Problem: new addresses start with an null id but get an id when attached to a person and this person gets merged() (re-attached) in the osiv-filter.
  • lazy compute the hashcode when hashCode() is first called and make that hashcode @Transitional. Does not work, as merge() returns a new object and the hashcode does not get copied over.

What i would need is an ID that gets assigned during object creation I think. What would be my options here? I don’t want to introduce some additional persistent property. Is there a way to explicitly tell JPA to assign an ID to an object?

Regards

  • 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-14T18:05:54+00:00Added an answer on May 14, 2026 at 6:05 pm

    Using the id of an entity is not a good idea because transient entities don’t have an id yet (and you still want a transient entity to be potentially equal to a persistent one).

    Using all properties (apart from the database identifier) is also not a good idea because all properties are just not part of the identity.

    So, the preferred (and correct) way to implement equality is to use a business key, as explained in Java Persistence with Hibernate:

    Implementing equality with a business key

    To get to the solution that we recommend, you need to understand the notion of
    a business key. A business key is a property, or some combination of properties, that
    is unique for each instance with the same database identity. Essentially, it’s the natural key that you would use if you weren’t using a surrogate primary key instead.
    Unlike a natural primary key, it isn’t an absolute requirement that the business
    key never changes—as long as it changes rarely, that’s enough.

    We argue that essentially every entity class should have some business key, even
    if it includes all properties of the class (this would be appropriate for some
    immutable classes). The business key is what the user thinks of as uniquely identifying a particular record, whereas the surrogate key is what the application and
    database use.

    Business key equality means that the equals() method compares only the properties that form the business key. This is a perfect solution that avoids all the problems described earlier. The only downside is that it requires extra thought to
    identify the correct business key in the first place. This effort is required anyway;
    it’s important to identify any unique keys if your database must ensure data integrity via constraint checking.

    For the User class, username is a great candidate business key. It’s never null,
    it’s unique with a database constraint, and it changes rarely, if ever:

        public class User {
            ...
            public boolean equals(Object other) {
                if (this==other) return true;
                if ( !(other instanceof User) ) return false;
                final User that = (User) other;
                return this.username.equals( that.getUsername() );
            }
            public int hashCode() {
                return username.hashCode();
            }
    }
    

    Maybe I missed something but for an Address, the business key would typically be made of the street number, the street, the city, the postal code, the country. I don’t see any problem with that.

    Just in case, Equals And HashCode is another interesting reading.

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

Sidebar

Ask A Question

Stats

  • Questions 441k
  • Answers 441k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to do: public class EnumerableDisposer<T, J> : IDisposable… May 15, 2026 at 5:31 pm
  • Editorial Team
    Editorial Team added an answer Well, these are Perl basics. You should start learning it… May 15, 2026 at 5:31 pm
  • Editorial Team
    Editorial Team added an answer Just use the Control's Focus method: TextBox1.Text = selectedItemText; TextBox1.Focus();… May 15, 2026 at 5:31 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.