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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:26:45+00:00 2026-05-29T21:26:45+00:00

I am using hibernate and need to override equals and hashCode(). I chose to

  • 0

I am using hibernate and need to override equals and hashCode(). I chose to use google-guava’s equals and hashCode helpers.

I wanted to know if I am missing something here.

I have get/set methods for idImage and filePath.

@Entity
@Table(name = "IMAGE")
public class ImageEntity {
    private Integer idImage;
    private String filePath;

    @Override
    public int hashCode() {
        return Objects.hashCode(getFilePath());
    }

    @Override
    public boolean equals(final Object obj) {
        if(obj == this) return true;
        if(obj == null) return false;

        if(obj instanceof ImageEntity){
            final ImageEntity otherImage = (ImageEntity) obj;
            return Objects.equal(getFilePath(), otherImage.getFilePath());
        }
        return false;
    }
}

EDIT:

Ran into inheritance and have a sample here

  • 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-29T21:26:48+00:00Added an answer on May 29, 2026 at 9:26 pm

    The problem with the instanceof operator is that it works taking into account polymorphism, if I may say so.

    Let’s say, for example, that you do this:

    public class AdvancedImageEntity extends ImageEntity
    {
        //some code here
    }
    

    and then you do this:

    ImageEntity ie = new ImageEntity ();
    AdvancedImageEntity advanced_ie = new AdvancedImageEntity ();
    
    boolean this_will_be_true = ie.equals (advanced_ie);
    

    As the name suggests, that equals call will return true, because of the instanceof operator.

    I know this sounds like basic stuff and most people know it, but it’s SOOOO damn easy to forget it. Now, if you want that behaviour, then it’s fine, you implemented equals correctly. But if you consider that an ImageEntity object must not be equal to an (hypothetical) AdvancedImageEntity object, then either declare ImageEntity to be final OR forget about instanceof and implement your equals method like this:

    @Override public boolean equals(final Object obj)
    {
        if(obj == this) return true;
        if(obj == null) return false;
    
        if (getClass ().equals (obj.getClass ()))
        {
            final ImageEntity otherImage = (ImageEntity) obj;
    
            return Object.equals (getFilePath(), otherImage.getFilePath());
        }
    
        return false;
    }
    

    This will check the object’s true type, no matter what type the reference is. If the obj parameter is an instance of a subclass, it would “slip” by instanceof. But getClass is a lot more strict and won’t allow it.

    PS: I’m not saying that instanceof is bad and should not be used. I’m just saying that you must be aware of this particular situation and decide whether to use it taking this into account.

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

Sidebar

Related Questions

I am using ClassMetadata to determine the structure of a hibernate POJO. I need
I'm using Hibernate and I need to find a way of retrieving the relations
I need to insert a lot of data in a database using hibernate, i
In my web app,I am using hibernate and hsqldb for database operations..and use Ant
i need to measure performance of my program unit. I am using hibernate as
Background: My REST service project was started up by using Hibernate. I use id
I am using hibernate.properties to configure my hibernate connection to mysql. But I need
I'm using Fluent NHibernate and need to get my Connection String from the connection.connection_string
I'm using NHibernate on a project and I need to do data auditing. I
I am using NHibernate as my ORM solution and have a need for a

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.