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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:58:57+00:00 2026-05-15T13:58:57+00:00

I have a domain object called User . Properties of user include ssoId, name,

  • 0

I have a domain object called User. Properties of user include ssoId, name, email, createdBy, createdDate and userRole. Of these, ssoId must be unique in the sense no two users can have the same sso id. So my equals method checks for the sso id and returns either true or false.

@Override public boolean equals(Object o) {
    if (!(o instanceof User))
      return false;
    return user.getSsoId().equals((User)o.getSsoId());
}

What I feel is that this is an incorrect implementation, though it is correct as far as the business rules are concerned. The above implementation will return true for two objects with same sso id but with different values for say name or email or both. Should I change my equals contract to check the equality of all fields? What is your suggestion?

  • 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-15T13:58:58+00:00Added an answer on May 15, 2026 at 1:58 pm

    This is (almost) correct for "technical equality", but not for "natural equality". To achieve top technical equality, you should also test the reflexive o == this. It may happen that the object isn’t persisted in DB yet and thus doesn’t have a technical ID yet. E.g.

    public class User {
    
        private Long id;
    
        @Override
        public boolean equals(Object object) {
            return (object instanceof User) && (id != null) 
                 ? id.equals(((User) object).id) 
                 : (object == this);
        }
    
        @Override
        public int hashCode() {
            return (id != null)
                 ? (User.class.hashCode() + id.hashCode())
                 : super.hashCode();
        }
    
    }
    

    For "natural equality" you should rather compare all non-technical properties. For "real world entities" this is after all more robust (but also more expensive) than technical equality.

    public class User {
    
        private String name;
        private Date birth;
        private int housenumber;
        private long phonenumber;
    
        @Override
        public boolean equals(Object object) {
            // Basic checks.
            if (object == this) return true;
            if (!(object instanceof User)) return false;
    
            // Property checks.
            User other = (User) object;
            return Objects.equals(name, other.name)
                && Objects.equals(birth, other.birth)
                && (housenumber == other.housenumber)
                && (phonenumber == other.phonenumber);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(name, birth, housenumber, phonenumber);
        }
    
    }
    

    True, that’s lot of code when there are a lot of properties. A bit decent IDE (Eclipse, Netbeans, etc) can just autogenerate equals(), hashCode() (and also toString(), getters and setters) for you. Take benefit of it. In Eclipse, rightclick code and peek the Source (Alt+Shift+S) menu option.

    See also:

    • JBoss: Equals and HashCode (in view of persistence)
    • Hibernate: Persistent Classes – implementing equals() and hashCode()
    • Related SO question: Overriding equals and hashCode in Java
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So let's say we have a domain object such as the following public class
I have a basic domain object, say like Person or Campaign or Event that
I have a property on a domain object that is declared in a many-to-one
I have the current basic structure for each domain object that I need to
Let's say I have an interface representing a domain object: public interface IFoo {
I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
I have a collection of domain objects that I need to convert into another
We have our domain model declared in rusty old hbm files, we wish to
I have a domain class containing a couple of fields. I can access them
I have a domain and a group of sub-domains that require authentication to access.

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.