关于在 equals 实现中包含域模型 id 的最佳实践
Best practice regarding the inclusion of id of domain model on equals implementation
关于在 grails 中实现域模型相等的最佳实践是什么?
我们是包含 id 字段还是只包含业务规则相关字段?
Hibernate 建议您在 equals 实现中只包含业务键/候选键。如果您生成了 id 字段,则在 equals 实现中包含 id 字段可能会产生负面影响。 Hibernate 仅在保存对象时分配 id(如果您使用生成的 id)。现在例如,如果您新的未保存的域对象在 HashSet 中并且您保存了该域,它将生成 id 并将其分配给该域,如果您的 equals / hashcode 基于 id 字段,则该域的哈希码将更改,并且您的域将在集合中丢失。
建议您使用唯一的不可变字段实现 equals。
查看参考资料
关于在 grails 中实现域模型相等的最佳实践是什么?
我们是包含 id 字段还是只包含业务规则相关字段?
Hibernate 建议您在 equals 实现中只包含业务键/候选键。如果您生成了 id 字段,则在 equals 实现中包含 id 字段可能会产生负面影响。 Hibernate 仅在保存对象时分配 id(如果您使用生成的 id)。现在例如,如果您新的未保存的域对象在 HashSet 中并且您保存了该域,它将生成 id 并将其分配给该域,如果您的 equals / hashcode 基于 id 字段,则该域的哈希码将更改,并且您的域将在集合中丢失。
建议您使用唯一的不可变字段实现 equals。
查看参考资料