title: hashCode() and equals() methods of a composite id class
body: <p>It's been days I'm trying to find the correct way to implement the equals() and hashCode() methods of a composite-id class.</p>
  
  <p>The trouble I'm facing when I try to update the main object (Gara) are: </p>
  
  <ol>
  <li>StackOverflow</li>
  <li>DuplicateKeyException: a different object with the same identifier value was already associated with the session</li>
  <li>org.hibernate.ObjectNotFoundException: No row with the given identifier exists</li>
  </ol>
  
  <p>My Composite-id class</p>
  
  <pre><code>@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
                  + "]";
      }
  
  
  }
  </code></pre>