gpt4 book ai didi

java - JPA : not overriding equals() and hashCode() in the entities?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:01:48 26 4
gpt4 key购买 nike

看完this article ,我倾向于不完全覆盖 equals() 和 hashCode()。

在那篇文章的总结中,关于 no eq/hC at all 列,唯一的结果是我无法进行如下比较操作:

  1. contains() 在分离实体列表中,或
  2. 比较来自不同 session 的相同实体

并期待正确的结果。

但我仍然有疑问,想问问你的经验,完全跳过 equals 和 hashCode 是否是一种不好的做法,还有什么我现在还不知道的其他后果。

只是另一个信息点,我倾向于使用 List Collections 而不是 Set。我的假设是,当存储在列表中时,我真的不需要重写 hashCode 和 equal。

最佳答案

阅读这篇关于该主题的非常好的文章:Don't Let Hibernate Steal Your Identity .

文章的结论是这样的:

Object identity is deceptively hard to implement correctly when objects are persisted to a database. However, the problems stem entirely from allowing objects to exist without an id before they are saved. We can solve these problems by taking the responsibility of assigning object IDs away from object-relational mapping frameworks such as Hibernate. Instead, object IDs can be assigned as soon as the object is instantiated. This makes object identity simple and error-free, and reduces the amount of code needed in the domain model.

关于java - JPA : not overriding equals() and hashCode() in the entities?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5191406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com