NHibernate set:我应该重写 Equals 和 GetHashCode 吗?

发布于 2024-08-31 13:27:54 字数 310 浏览 2 评论 0 原文

我是 NHibernate 的新手。我正在使用<设置...>一些多对一和多对多关联的映射。这些被公开为 ICollection 类型的属性,实际上由 HashSet 实现。

我的问题是,我是否应该重写相关类型的 Equals 和 GetHashCode ,以便它们与类型的数据库标识匹配(实际上,当 Id 属性相等时,对象相等)?或者 NHibernate 是否以某种方式为我处理这个问题?

如果这样做,如果我想在任意时间在多对多集合中插入多个值,就会出现问题,因为新元素可能具有 Guid.Empty 的 Id;因此被视为同一项目。

I am new to NHibernate. I am using <set ... > mapping for some many-to-one and many-to-many associations. These are exposed as properties of type ICollection<T>, in practice implemented by HashSet<T>.

My question is, should I override Equals and GetHashCode for the related types, so they match the database identity of the types (in practice so that the objects are equal when the Id property is equal) ? Or does NHibernate handle this for me in some way ?

If I do this, I see a problem if I want to insert multiple values in the many-to-many collection at any one time, because the new elements might have the Id of Guid.Empty; and therefore be considered the same item.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰之心 2024-09-07 13:27:54

You don't have to run into that problem necessarily, take a look at the AbstractEntity in uNhAddIns (which implements Equals and GetHashCode)

谜兔 2024-09-07 13:27:54

如果您要从两个不同的 Nhibernate 会话加载对象,请覆盖 Equals/GetHashCode。查看 Nhibernate 手册

Override Equals/GetHashCode if you are loading objects from two different Nhibernate sessions. Check out the Nhibernate manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文