[jboss-dev-forums] [EJB3 Development] - Entity hashCode and equals overriding
fabio valsecchi
do-not-reply at jboss.com
Tue Jan 1 13:21:30 EST 2013
fabio valsecchi [https://community.jboss.org/people/fabiovalse] created the discussion
"Entity hashCode and equals overriding"
To view the discussion, visit: https://community.jboss.org/message/786963#786963
--------------------------------------------------------------
Hi there,
I'm having some problems overring hashCode() and equals() methods of an entity Book. This is my code:
------------------------------------------------------------------------
@Override
public int hashCode() {
int hash = 1;
hash = hash * 31 + this.isbn.hashCode();
return hash;
}
@Override
public boolean equals(Object o) {
if (o==this)
return true;
if (o instanceof Book) {
return (this.isbn == ((Book) o).getIsbn());
}
return false;
}
------------------------------------------------------------------------
The problem is that when I try to add a book to a hashMap like here:
------------------------------------------------------------------------
@Override
public void addArticle(Book book, Integer quantity) {
Integer *currentQuantity* = cart.getArticles().get(book);
System.out.println(currentQuantity);
if (currentQuantity == null) {
currentQuantity = 0;
}
currentQuantity += quantity;
cart.getArticles().put(book, currentQuantity);
}
------------------------------------------------------------------------
The *currentQuantity* Integer is always null even if in the hashMap there's already the Book that i'm adding.
Maybe it's a problem of my hashCode and equals implementation but i've tried using a hashCode() that return always 1 and with an equals that return always true and *currentQuantity* is still null.
Can someone give me a hint?
Thank you in advance
--------------------------------------------------------------
Reply to this message by going to Community
[https://community.jboss.org/message/786963#786963]
Start a new discussion in EJB3 Development at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2030]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20130101/11ab9d1f/attachment.html
More information about the jboss-dev-forums
mailing list