NHibernate set : Should I override Equals and GetHashCode?
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开发者_Go百科 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.
You don't have to run into that problem necessarily, take a look at the AbstractEntity
in uNhAddIns (which implements Equals
and GetHashCode
)
Override Equals/GetHashCode if you are loading objects from two different Nhibernate sessions. Check out the Nhibernate manual.
精彩评论