是否有一个样品为什么的Equals / GetHash code应该在NHibernate的覆盖? [英] Is there a sample why Equals/GetHashCode should be overwritten in NHibernate?

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

问题描述

我找了很多帖子,其中解释,一要始终覆盖在NHibernate的实体类的equals / GetHash code的。如果我不使用套,这真的有必要吗?

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?

我根本无法找到它表明缺少的Equals / GetHash code可能会导致意外和错误的行为的样本。似乎一切都完美地工作,没有他们。这真是奇怪,大家说,这是必要的,但没有人能提供样本,显示的为什么的,这是必要的。

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 .

推荐答案

有一个<一个href="http://stackoverflow.com/questions/11253529/nhibernate-n1-with-ternary-relationship-want-the-middle-entity-in-the-ternary">question在SO最近有关NHibernate的做选择N + 1即使读取指定。问题 的是缺少等于 / GetHash code 的实施。

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.

答案链接到另一个类似的<一个href="http://stackoverflow.com/questions/10176862/nhibernate-producing-proxy-despite-hql-fetch">question.

The answer links to another similar question.

下面的<一个href="http://stackoverflow.com/questions/5851398/nhibernate-reasons-for-overriding-equals-and-gethash$c$c">another问题推理背后等于 / GetHash code 覆盖。

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

<一个href="http://stackoverflow.com/questions/11253529/nhibernate-n1-with-ternary-relationship-want-the-middle-entity-in-the-ternary">Nhibernate n + 1个三元关系。要在三元
中间实体 <一href="http://stackoverflow.com/questions/10176862/nhibernate-producing-proxy-despite-hql-fetch">Nhibernate生产代理尽管HQL获取
<一href="http://stackoverflow.com/questions/5851398/nhibernate-reasons-for-overriding-equals-and-gethash$c$c">NHibernate:理由重写Equals和GetHash code
为什么等于和GetHash code是如此重​​要NHibernate的
<一href="http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethash$c$c-when-equals-method-is-overriden-in-c">Why是很重要的覆盖GetHash code当equals方法的重写在C#?

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 overriden in C#?

修改

您并不需要重写他们所有的时间。如果您使用的是组合键,使用分离的实体或无状态会话多个会话可能是必要的。

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.

如果您正在使用一个会话只,NHibernate的商店使用标识映射的实体一级缓存。在这种情况下,实体比较是通过比较的ID进行。

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.

在上面(独立实体,无状态会话)的情况下,NHibernate的比较实际的实体,而不是他们的ID。默认情况下,的Object.Equals 做参考平等。因此,两个对象是相等的,如果他们指向完全相同的实例。你可能有两个实例具有相同的标识,但的Object.Equals 将返回他们。这是与实体对比 定义

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有一个很好的解释等于哈希code 少code的例子。

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

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

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