Reputation: 403
I have two objects from database (in database it is same object), but they have different hashes:
GroupType groupType = groupTypeDao.findById(3);
GroupType groupType1 = groupTypeDao.findById(3);
System.out.println(groupType);
System.out.println(groupType1);
I get this output:
GroupType@6040
GroupType@6041
Why is that? Technology stack: Spring, JavaFX, Hibernate.
I have another project with Spring and Hibernate. Configuration files are identical in the two projects. Hibernate version is identical also. But in another project this produce same hashcodes.
Upvotes: 0
Views: 398
Reputation: 1014
System.out.println(groupType)
usually calls the toString()
method on java.lang.Object and this prints:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Now, hashCode()
may be a little bit misleading because if it's not overwritten then ...
the hashCode method defined by class Object does return distinct integers for distinct objects.
Quotes from java.lang.Object documentation.
Upvotes: 0
Reputation: 308733
What you've printed are object references. They are indeed different if you created each reference by calling new.
You need to override equals, hashCode, and toString according to "Effective Java" to get the behavior you want.
Upvotes: 4