Search code examples
javaspringhibernatecollectionshibernate-mapping

Spring with Hibernate Integration getting duplicate records


enter image description here

In our project we are using Spring with Hibernate and we are using other team jars as dependent jars to fetch data from oracle data base.

We are getting duplicate records even though we implemented equals() and hashCode() methods in model classes and we are storing these object in HashSet. I am strange why set is allowing duplicate objects here. Even though hashCode() and equals() method is overridden ?.

If you observe the screen shot modcount size is different and table size is different


Solution

    • remove hashCode() or equals() from your class

    • if you are Override equals and hashcode methods in your class, then equal objects return the same hashCode.. the HashSet is not allowed the same hashcode because HashSet validating duplicate in based on hashCode, so the solution for you: you can remove hashCode() or equals() from your class