是否有示例为什么应在 NHibernate 中覆盖 Equals/GetHashCode? [英] Is there a sample why Equals/GetHashCode should be overwritten in NHibernate?

查看:23
本文介绍了是否有示例为什么应在 NHibernate 中覆盖 Equals/GetHashCode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很多帖子都解释说应该始终覆盖 NHibernate 实体类上的 Equals/GetHashCode.如果我不使用 Sets,这真的有必要吗?

我根本找不到一个示例,表明缺少 Equals/GetHashCode 会导致意外和错误的行为.如果没有它们,一切似乎都可以完美运行.这真的很奇怪,每个人都说这是必要的,但没有人可以提供一个示例来说明为什么需要这样做.

解决方案

有一个 问题 最近关于 NHibernate 执行 select N+1 即使指定了 fetch.问题缺少Equals/GetHashCode 实现.>

答案链接到另一个类似的问题.

这里是另一个问题,关于Equals/GetHashCode 覆盖.

Nhibernate n+1 with三元关系.想要三元中的中间实体
Nhibernate 生产代理尽管 HQL fetch
NHibernate:覆盖 Equals 和 GetHashCode 的原因
为什么 Equals 和 GetHashCode 对 NHibernate 如此重要
为什么重要当覆盖 Equals 方法时覆盖 GetHashCode?

<小时>

编辑

您不需要一直覆盖它们.如果您使用复合键、具有分离实体的多个会话或无状态会话,则可能有必要.

如果您只使用单个会话,NHibernate 会使用身份映射将实体存储到一级缓存.这种情况下的实体比较是通过比较 id 来完成的.

在上述情况下(分离实体、无状态会话),NHibernate 比较实际实体,而不是它们的 id.默认情况下,Object.Equals 执行引用相等.因此,如果两个对象指向完全相同的实例,则它们是相等的.您可能有两个具有相同标识的实例,但 Object.Equals 将为它们返回 false.这与 Entity 定义形成对比:><块引用>

一个不是由其属性定义的对象,而是由一个连续性的线索及其身份.

JBoss Hibernate wiki 对 Equals很好的解释HashCode 包含少量代码示例.

I find a lot of posts where it is explained that one should always override Equals/GetHashCode on a NHibernate entity class. If I don't use Sets, is this really necessary?

I simply can't find a sample where it is shown that missing Equals/GetHashCode can lead to unexpected and wrong behaviour. Everything seems to work perfectly without them. This is really strange that everyone says this is necessary but no one can provide a sample which shows why this is needed .

解决方案

There was a question on SO recently about NHibernate doing select N+1 even if fetch is specified. The problem was with missing Equals / GetHashCode implementation.

The answer links to another similar question.

Here's another question on reasoning behind Equals / GetHashCode overrides.

Nhibernate n+1 with ternary relationship. Want the middle entity in the ternary
Nhibernate producing proxy despite HQL fetch
NHibernate: Reasons for overriding Equals and GetHashCode
Why Equals and GetHashCode are so important to NHibernate
Why is it important to override GetHashCode when Equals method is overridden?


Edit

You don't need to override them all the time. It may be necessary if you are using composite keys, multiple sessions with detached entities or stateless sessions.

If you are working with a single session only, NHibernate stores the entities to first level cache using an identity map. Entity comparison in that case is done by comparing ids.

In cases above (detached entity, stateless session), NHibernate compares actual entities, not their ids. By default, Object.Equals does reference equality. So two objects are equal if they point to the exact same instance. You might have two instances with the same identity, but Object.Equals would return false for them. This is in contrast with the Entity definition:

An object that is not defined by its attributes, but rather by a thread of continuity and its identity.

JBoss Hibernate wiki has a good explanation on Equals and HashCode with few code examples.

这篇关于是否有示例为什么应在 NHibernate 中覆盖 Equals/GetHashCode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆