Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top