assertEquals(def.getMengs(), exp.getMengs());
失敗,報告: 預期:java.util.HashSet中< [...所以geht模具的Légende......傳說...]>卻被:java.util.HashSet中< [ ...所以geht死Legende ......傳說有它...]>爲什麼我的等號方法不起作用?
事實上,通過調試器,我發現這兩個集合都只包含一個objId = 1的含義。 我期望在含義類(@Entity)中的下列代碼來保證上述工作。
@Override
public boolean equals(Object object) {
if (!(object instanceof Meaning)) {
return false;
}
Meaning other = (Meaning) object;
if (other != null && objId == other.objId) return true;
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 67 * hash + this.objId;
return hash;
}
事實上,這種測試通過:
db.insert(admin);
final Meaning meng = new Meaning(admin, new Expression("essen"));
meng.setObjId(11);
final Meaning meng1 = new Meaning(admin, new Expression("mangiare"));
meng1.setObjId(11);
assertEquals(meng,meng1);
那麼,什麼可能是我的問題嗎? Thery都是HashSets,它們都是相同的大小,並且它們內部的對象相等。確實
assertEquals(def.getMengs().iterator().next(), exp.getMengs().iterator().next());
它通過之前。然而,這不會(但我不知道爲什麼):
assertTrue(def.getMengs().containsAll(exp.getMengs()));
所以,這是問題所在。
下面是測試代碼:
try{
db.insertWords(toEnumMap(mengs[i],admin));
}catch(Exception e){
fail(e.getMessage());
}
final Expression exp = db.get(Expression.class, mengs[i][0]);
testGender(exp, mengs[i][2]);
final Expression def = db.get(Expression.class, mengs[i][1]);
assertNotNull(def);
assertEquals(def.getMengs().iterator().next(), exp.getMengs().iterator().next());
assertEquals(exp.getMengs().size(), def.getMengs().size());
assertTrue(def.getMengs().containsAll(def.getMengs()));
assertTrue(def.getMengs().containsAll(exp.getMengs()));
assertEquals(def.getMengs(), exp.getMengs());
db.get方法只是包裝em.find。 InsertWords應該保持def和exp。
public void insertWords(EnumMap<Input, MemoEntity> input) throws MultipleMengsException {
insert(input.get(Input.expression)); //INSERT OR IGNORE
final boolean isNewDef = insert(input.get(Input.definition));
final Expression def = get(Expression.class, input.get(Input.definition).getId());
final Expression exp = get(Expression.class, input.get(Input.expression).getId());
final MUser usr = get(MUser.class, input.get(Input.user).getId());
final Set<Meaning> mengs = getMengs(usr,def,isNewDef);
if (mengs == null) {//is new to the user
final Meaning meng = new Meaning(usr, exp, def);
insert(meng);
} else { //old meaning
if (mengs.size() > 1) throw new MultipleMengsException(mengs);
else{
final Meaning meng = mengs.iterator().next();
meng.addExp(exp);
meng.setLastPublishedDate(null); //reschedule
}
}
Logger.getAnonymousLogger().log(Level.INFO, "inserted pair <{0},{1}>", new String[]{exp.getExpression(), def.getExpression()});
}
public boolean insert(final MemoEntity entity) {
if (em.find(entity.getClass(), entity.getId()) == null) {
et.begin();
em.persist(entity);
et.commit();
return true;
}
return false;
}
public <MemoEntity> MemoEntity get(final Class<MemoEntity> entityClass, final Object primaryKey) {
return em.find(entityClass, primaryKey);
}
'int hash = 7;哈希= 67 *哈希'?爲什麼不'int hash = 469 + obj.Id'?或者只是'返回obj.Id'?向散列碼添加常數是不必要的, – NullUserException 2010-07-27 14:03:34
@NullUserException:'return 4;'是正確的變體! – Roman 2010-07-27 14:05:08