在Mongo实体类上覆盖hashCode()和equals()方法是否安全?

huangapple 未分类评论47阅读模式
英文:

Is it safe to override hashCode() and equals() on Mongo entity class

问题

你能帮助我吗?

我有一个Mongo实体,其中包含Mongo自动生成的_id

我想要一种更容易的机制来比较这些实体,排除_id以进行比较。

我正在使用Lombok,所以添加了注解

@EqualsAndHashCode(exclude=“_id”)

我没有发现这对于从Mongo创建/保存/查询这些项有任何影响,但也许我漏掉了一些东西

equals()hashCode()中排除_id是否可以?这会影响Mongo如何保存它们吗?
(我在该实体上有另一个业务键,但无论如何我需要自动生成_id

英文:

Can you please help me?

I have Mongo entity which contains _id self generated by Mongo.

I would like to have easier mechanism of comparing these entities by excluding this _id from comparison.

I’m using Lombok, so added annotation

@EqualsAndHashCode(exclude=“_id”)

I did not find any influence of this on create/save/query these items from Mongo, but maybe I miss something

Is it OK to exclude _id from equals() and hashCode()? Does it take influence on how they will be saved by Mongo?
(I have another buisiness key on that entity but I need _id self generated anyway)

huangapple
  • 本文由 发表于 2020年6月29日 15:02:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/62632838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定