Is there a sample why Equals/GetHashCode should be overwritten in NHibernate?

后端 未结 2 1797
迷失自我
迷失自我 2021-01-02 19:48

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?

2条回答
  •  遥遥无期
    2021-01-02 20:13

    In fact, there are only rare cases when it leads to side effects. But if you get them, they are quite subtle. Apart from composite primary keys and dictionary keys, which always require a correct Equals / GetHashCode implementation.

    NH cares about instantiating an entity only once in memory, so the default reference comparison should work ... if there wouldn't be lazy loading.

    When not overriding Equals, you get problems when dealing with proxies. There are always two instances: the proxy and the real entity. They both represent the same entity. Only with a correctly implemented Equals method it is treated as the same.

提交回复
热议问题