Jpaエンティティ(MyEntity)に合成キーがある場合は、equals()とhashCode ()IDClass(この場合はID)に?重複とみなされますか?またはMyEntityの "equals and hashCode()"では、IDクラス[return MyEntity.ID(id1、id2).hashCode();}を呼び出す必要があります。 MyEntityのhashCode()にありますか?jpaエンティティのIdClass(合成キー)でequals()とhashCode()をオーバーライドする必要があります
@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;
}
}
私は両方のクラスで言う – XtremeBaumer