Thursday, November 15, 2007

Nhibernate and Composite Keys

I like using Nhibernate to do my object relational mapping. Although there is a large learning curve to the tool, once you use it a few times, mapping your objects become second nature, or that is what I thought until I had to map a database heavily using composite keys.

See for some reason when using composite keys NHibernate has trouble telling if objects are new or if they are updates. This force you to have to call the insert method instead of the save. Not a big deal if you can tell when a page is doing an insert and when it id doing an update.

Another issue was getting many-to-one mappings and one-to-many mappings to work. see loading was not a problem but again when trying to update and save I ran into all sorts of problems. The quick solution was to all those mappings out of the mapping file and load them in my DAO layer. Not a big deal but when you are using to having a tool handle all that code for you it is upsetting.

All in all I was able to make the tool work. Now that I know the tricks, I wont feel as much pain the next time I have to map a composite key system.
Post a Comment