title: Do I need to override equals() and hashCode() in the IdClass (composed key) of a jpa Entity
body: <p>If I have a composed key in a Jpa Entity (MyEntity), do I need to add equals() and hashCode() in the IdClass (ID in this case)? It is considered as a duplication? or in the MyEntity "equals and hashCode()", I have to call those of ID class [return new MyEntity.ID(id1,id2).hashCode(); in the hashCode() of MyEntity] ?</p>
  
  <pre><code>@Entity
  @IdClass(MyEntity.ID.class)
  public class MyEntity implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      private long id1;
      @Id
      private long id2;
  
      private String otherField;
      public static class ID implements Serializable {
          private static final long serialVersionUID = 1L;
          private long id1;
          private long id2;
          public ID() {
              super();
          }
          public ID(long id1, long id2) {
              super();
              this.id1 = id1;
              this.id2 = id2;
          }
          public long getId1() {
              return id1;
          }
          public void setId1(long id1) {
              this.id1 = id1;
          }
          public long getId2() {
              return id2;
          }
          public void setId2(long id2) {
              this.id2 = id2;
          }
          @Override
          public int hashCode() {
              final int prime = 31;
              int result = 1;
              result = prime * result + (int) (id1 ^ (id1 &gt;&gt;&gt; 32));
              result = prime * result + (int) (id2 ^ (id2 &gt;&gt;&gt; 32));
              return result;
          }
          @Override
          public boolean equals(Object obj) {
              if (this == obj) {
                  return true;
              }
              if (obj == null) {
                  return false;
              }
              if (getClass() != obj.getClass()) {
                  return false;
              }
              ID other = (ID) obj;
              if (id1 != other.id1) {
                  return false;
              }
              if (id2 != other.id2) {
                  return false;
              }
              return true;
          }
      }
      public MyEntity() {
          super();
      }
      public long getId1() {
          return id1;
      }
      public void setId1(long id1) {
          this.id1 = id1;
      }
      public long getId2() {
          return id2;
      }
      public void setId2(long id2) {
          this.id2 = id2;
      }
      public String getOtherField() {
          return otherField;
      }
      public void setOtherField(String otherField) {
          this.otherField = otherField;
      }
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + (int) (id1 ^ (id1 &gt;&gt;&gt; 32));
          result = prime * result + (int) (id2 ^ (id2 &gt;&gt;&gt; 32));
          result = prime * result + ((otherField == null) ? 0 : otherField.hashCode());
          return result;
      }
      @Override
      public boolean equals(Object obj) {
          if (this == obj) {
              return true;
          }
          if (obj == null) {
              return false;
          }
          if (getClass() != obj.getClass()) {
              return false;
          }
          MyEntity other = (MyEntity) obj;
          if (id1 != other.id1) {
              return false;
          }
          if (id2 != other.id2) {
              return false;
          }
          if (otherField == null) {
              if (other.otherField != null) {
                  return false;
              }
          } else if (!otherField.equals(other.otherField)) {
              return false;
          }
          return true;
      }
  }
  </code></pre>