The answer is pretty simple and straight forward: These are different concepts.
The Equals
and GetHashCode
are C# (application, business) way how to distinguish objects in runtime. It is an operator and calulated value ... both of them not persisted.
NHibernate does not know what should be compared on the DB level.
But what is more interesting in the snippet above is this part:
MyObject contract = new MyObject( some parameters );
The some parameters could mean two things: 1) identifier or 2) dynamic searching filters.
In the first case we should profit form the surrogated keys / id:
Session.Get<MyObject>(id);
In the second, we'll be most likely (sooner or later for sure) be provided with a changing set of values. And then we can apply them dynamically.
Finally, the case you are most likely searching for is some GetByCode or GetByName. I would say, that implementing method like this
public virtual MyObject GetByCode(string code)
{
var result = Session
.Query<MyObject>()
.Where( x => x.Code == code )
.FirstOrDefault();
return result;
}
will NOT make your code dirty. In fact, this method could be on a Data layer. So, it could have later more setings (filters by user language... etc). While the Equals
and GetHashCode
belongs to Entity (POCO if possible), and should not be so dynamic.