assertEquals(def.getMengs(), exp.getMengs());
fails, reporting: expected: java.util.HashSet<[...so geht die Legende ... ...legend has it ... ]> but was: java.util.HashSet<[...so geht die Legende ... ...legend has it ... ]>
Indeed, through the debugger I see that both sets contain only one Meaning with objId = 1 for both. I expected the following code in Meaning class (@Entity) to guarantee that the above works.
@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;
}
Indeed, this test passes:
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);
So what could be my problem? Thery are both HashSets, they are both of the same size, and the objects inside them equals. Indeed
assertEquals(def.getMengs().iterator().next(), exp.getMengs().iterator().next());
before it passes. However this won't (but I don't know why):
assertTrue(def.getMengs().containsAll(exp.getMengs()));
So, it's the problem.
Here's the test code:
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 just wraps em.find. InsertWords should be persisting def and 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);
}