Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hashCode() and equals() methods of a composite id class

It's been days I'm trying to find the correct way to implement the equals() and hashCode() methods of a composite-id class.

The trouble I'm facing when I try to update the main object (Gara) are:

  1. StackOverflow
  2. DuplicateKeyException: a different object with the same identifier value was already associated with the session
  3. org.hibernate.ObjectNotFoundException: No row with the given identifier exists

My Composite-id class

@Embeddable 
public class GaraAgenziaId implements Serializable {

    private static final long serialVersionUID = 4934033367128755763L;

    static Logger logger = LoggerFactory.getLogger(GaraAgenziaId.class);

    private Gara gara;

    private Agenzia agenzia;

    @ManyToOne
    public Gara getGara() {
        return gara;
    }

    public void setGara(Gara gara) {
        this.gara = gara;
    }

    @ManyToOne
    public Agenzia getAgenzia() {
        return agenzia;
    }

    public void setAgenzia(Agenzia agenzia) {
        this.agenzia = agenzia;
    }


    @Override
    public String toString() {
        return "GaraAgenziaId [Gara=" + gara + ", agenzia=" + agenzia
                + "]";
    }


}
like image 586
SaganTheBest Avatar asked Jul 15 '14 08:07

SaganTheBest


People also ask

What is the difference between hashCode () and equals () methods?

Java hashCode() An object hash code value can change in multiple executions of the same application. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.

What is the hashCode () and equals () used for?

The equals() and hashcode() are the two important methods provided by the Object class for comparing objects. Since the Object class is the parent class for all Java objects, hence all objects inherit the default implementation of these two methods.

What is the relationship between hashCode () and equals () method in Java?

General contract associated with hashCode() methodIf two objects are equal(according to equals() method) then the hashCode() method should return the same integer value for both the objects.

Which class does override the equals () and hashCode () methods?

The Team class overrides only equals(), but it still implicitly uses the default implementation of hashCode() as defined in the Object class. And this returns a different hashCode() for every instance of the class.


1 Answers

these seems to work very well:

 public boolean equals(Object o) {
        if (this== o) return true;
        if (o ==null|| getClass() != o.getClass()) return false;

        GaraAgenziaId that = (GaraAgenziaId) o;

        if (gara !=null?!gara.equals(that.gara) : that.gara !=null) return false;
        if (agenzia !=null?!agenzia.equals(that.agenzia) : that.agenzia !=null)
            return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (agenzia !=null? agenzia.hashCode() : 0);
        result =31* result + (gara !=null? gara.hashCode() : 0);
        return result;
    }   
like image 131
SaganTheBest Avatar answered Oct 20 '22 20:10

SaganTheBest