views:

221

answers:

1

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.

+1  A: 

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

Mauricio Scheffer
So the answer is, that Equals and GetHashCode should be overridden for the entities ?
driis
Yes, it's recommended that you override them.
Mauricio Scheffer