Si j'ai une clé composée dans une entité Jpa (MyEntity), dois-je ajouter equals() et hashCode() dans l'IdClass (ID dans ce cas)? Il est considéré comme une duplication? ou dans MyEntity "equals et hashCode()", je dois appeler ceux de la classe ID [return new MyEntity.ID (id1, id2) .hashCode(); dans le hashCode() de MyEntity]?Ai-je besoin de redéfinir equals() et hashCode() dans l'IDClass (clé composée) d'un jpa Entité
@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 >>> 32));
result = prime * result + (int) (id2^(id2 >>> 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 >>> 32));
result = prime * result + (int) (id2^(id2 >>> 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;
}
}
je dirais dans les deux classes – XtremeBaumer