Hi folks,
I am facing a strange problem I can not figure out. Maybe you guys can give an explaination (or better, a fix!).
I have a class A that holds a map of class B for values, class K is used for keys :
public class A extends BaseObject implements Serializable {
private Map<K, B> m = new HashMap<K, B>();
public B getValue(String aString){
K key = new K();
k.setString(aString);
return m.get(k);
}
...
}
I have written a custom hashcode() method for the class K which is based on the getString() method only. So 2 different instances of K will have the same hashCode() result provided they have the same getString() result.
Now here is a simple test case :
public class MyTest extends AbstractTransactionalDataSourceSpringContextTests{
private ADao adao;
private KDao kdao;
public void setADao(ADao dao) {this.adao = dao;}
public void setKDao(KDao dao) {this.kdao = dao;}
public void testFails() throws Exception {
A a = adao.get(1L);
assertnotNull(a.getValue("foo")); // Fails!
}
public void testSuccessfull() throws Exception {
A a = adao.get(1L);
K k = kdao.getK("foo");
assertnotNull(a.getValue("foo"));
}
}
the first one fails whereas the second one is successfull... The only difference between them is that I load the K instance from DB before accessing the map (but I don't use this loaded object). Is there a reason for that?
Any explaination is warmly welcome.
Thanks.
|