私のMavenテストを実行するには少し問題があります。私のassertEquals(Set1、Set2)は動作しません。Set1が等しい場合でもunknowの理由Set2は自動的にアサート失敗を返します。Set.equalsは機能しません:最初のハッシュセットのみを確認してください
私はちょっと調べましたが、assertEquals(Set1、Set2)はSet1.equals(Set2)を呼び出し、Set1.equals(Set2)は2つのメソッドを呼び出す2つの要素の要素を比較します。.hashCode ()と.equals()です。だから私はいくつかのSystem.out.printlnを置いて、すべてが必要であるかどうかを調べる。しかし、関数.hashCodeは1回だけ呼ばれ、私はなぜそれを知りません。ここで
いくつかのコード:
System.out.println("assertProblem");
assertEquals(Set1, Set2);
。
if(Set1.equals(Set2))
System.out.println("equals");
else
System.out.println("not equals");
私のセットを含めることができるエンティティの2種類:私の2セットの
@Override
public int hashCode() {
System.out.println("hashCode");
int hash = 1;
hash *= id;
if(derogation != null)
hash *= derogation.getId();
if(description != null)
hash *= description.hashCode();
return hash;
}
@Override
public boolean equals(Object object) {
System.out.println("equals");
[...]
}
-----------------------------------------------
@Override
public int hashCode() {
System.out.println("hashCode2");
int hash = 1;
hash *= id;
if(derogation != null)
hash *= derogation.getId();
if(chemin != null)
hash *= chemin.hashCode();
if(userFam != null)
hash *= userFam.hashCode();
if(serveur != null)
hash *= serveur.hashCode();
if(type != null)
hash *= type.hashCode();
return hash;
}
@Override
public boolean equals(Object object){
System.out.println("equals2");
[...]
}
値:
[{id:3658, derogationId:657, description:description ! 0}, {id:3659, derogationId:657,chemin: chemin0, userFam:User/Family0, serveur:serveur010, type:ecriture}, {id:3660, derogationId:657, description:description ! 1}, {id:3661, derogationId:657,chemin: chemin1, userFam:User/Family1, serveur:serveur011, type:ecriture}, {id:3662, derogationId:657, description:description ! 2}, {id:3663, derogationId:657,chemin: chemin2, userFam:User/Family2, serveur:serveur012, type:ecriture}]
[{id:3658, derogationId:657, description:description ! 0}, {id:3659, derogationId:657,chemin: chemin0, userFam:User/Family0, serveur:serveur010, type:ecriture}, {id:3660, derogationId:657, description:description ! 1}, {id:3661, derogationId:657,chemin: chemin1, userFam:User/Family1, serveur:serveur011, type:ecriture}, {id:3662, derogationId:657, description:description ! 2}, {id:3663, derogationId:657,chemin: chemin2, userFam:User/Family2, serveur:serveur012, type:ecriture}]
そして結果:
assertProblem
hashCode
not equals
誰か助けてくれますか?
thx
これらは以下のとおりです:org.hibernate.collection.internal.PersistentSet – Antoine
不等号である場合、 'set1.equals(set2)'だけが呼び出されます。私の疑念は、何か怪しいものがあるということです。それは、その状態(変更されたハッシュコード)を変更したものか何かを変更したものです。 –
"カスタム" hashCodeメソッドが間違った方法で実装されていると思います。または、いくつかの要素をセットの1つに追加し、その後、そのハッシュ/キーのプロパティを変更します。その休止状態が両方のセットが確実に初期化されているかどうか確認します。 –