Hello,
After reading about Hibernate's documentation and a popular
http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java in StackOverflow, I understand that:
1) PK should never be used in hashCode() because it changes once it is persisted. Using PK in hashcode will also make it have different
state after persistence than before.
2) It is okay to use PK in equals() method as it can be used to compare the uniqueness of two objects.
MY PROBLEM
==========
I have a project where my java class has overriden version of equals() and hashCode() generated by Spring(Eclipse). When using the generator wizard for equals(), I chose not to include Id(PK) for comparison. My Id is an auto-generated value (@Generated). I completely understand that hibernate assigns Id value once the object is persisted i.e. if I now use my overridden equals(), I can compare to objects to be the same. To test:
1) I added multiple clone objects with all the field values same. One of the field is of Date class and I used same value "01-01-2012 11:00 PM" for all the dates. I say clone because except the Id, everything else is the same. I added 10 clone items.
2) When I perform a query saying that I need all created items that are created on or before "01-01-2012 11:00 PM", I surely expected all of them to be returned to me. and to make sure that I am not having any duplicates, I was storing the query results in HashSet object. What I got from the query results is ONLY one object which was created LASTLY i.e the Id was 10. Why is that?
Could someone please explain what's actually going on? ALSO, does it mean that if I retrieve an object edit it and then persist it back again, it won't be updated but stored as a new object?