NHibernate set: should I override Equals and GetHashCode?

I am new to NHibernate. I use the <set ...> mapping for some many-to-one and many-to-many associations. They are shown as properties of type ICollection <T>; in practice, HashSet <T> are implemented.

My question is: should I override Equals and GetHashCode for related types so that they match the identifier of the type database (in practice, so that the objects are equal when the Id property is equal)? Or is NHibernate handling this for me somehow?

If I do this, I see a problem if I want to insert multiple values ​​into the many-to-many collection at any time, because new elements may have the identifier Guid.Empty; and therefore be considered the same subject.

+4
source share
2 answers

You don't have to face this problem, see AbstractEntity in uNhAddIns (which implements Equals and GetHashCode )

+2
source

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

+1
source

Source: https://habr.com/ru/post/1308742/


All Articles