Thursday, March 19, 2009 11:23 AM
Im working with the new version (2.0 GA) of NHibernate. The problem I have encountered today is about the composite-id.
Lets say we have an entity that doesnt have a personal id field. We dont want to use the GUID or any other auto id generator class. We must implement the primary key logic in the corresponding table in the database. Now, imagine to have this table:
|Key||Column Name||SQL Type||Not null|
At this point we will have a mapping XML in NHibernate in this way:
If we compile the DAL of our project, we will receive a fancy error:
composite-id class must override Equals()
composite-id class must override GetHashCode()
The explanation is very simple. We are saying that this class has 3 fields to implement a comparison so the NHB must know how you can compare these fields and off course the CLR doesnt know how to do it.
This is the solution in our entity:
There we go!! Now you have implemented a full version of the method Hey is this the entity Im going to save or is this entity brand new?!?!
In my opinion NHB should be able to override this method by itself via reflection and not to ask you to rebuild thousand of entities if you want to use the standard pattern active Record.