Spring 使用 Hibernate Integration 获取重复记录

Spring with Hibernate Integration getting duplicate records

在我们的项目中,我们使用 Spring 和 Hibernate,我们使用其他团队 jar 作为依赖 jar 从 oracle 数据库中获取数据。

即使我们在模型 类 中实现了 equals() 和 hashCode() 方法,并且我们将这些对象存储在 HashSet 中,我们仍然得到重复的记录。我很奇怪为什么 set 在这里允许重复对象。即使 hashCode() 和 equals() 方法被重写了?.

如果您观察屏幕截图 modcount 大小不同 table 大小不同

  • 从您的 class

  • 中删除 hashCode() 或 equals()
  • 如果您在 class 中重写 equals 和 hashcode 方法,那么 equal objects return 相同的 hashCode.. HashSet 不允许使用相同的 hashcode,因为 HashSet 正在验证基于 hashCode 复制,因此适合您的解决方案:您可以从 class

  • 中删除 hashCode() 或 equals()