driis
driis

Reputation: 164311

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 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.

Upvotes: 4

Views: 1429

Answers (2)

burnt1ce
burnt1ce

Reputation: 14897

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

Upvotes: 1

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99740

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

Upvotes: 2

Related Questions