Geeks With Blogs
Mike Nichols - SonOfNun Technology If I were the captain on a big steamboat...

One of the difficult things about NHibernate is just getting started. While it is true that there is lots of docs and writings about her older brother Hibernate, the fact is that the documentation for NHibernate is really bad. It is true that it is just a port of Hibernate, so you can get thru it just studying the Hibernate docs. The big one to have is Hibernate in Action.

On occasion, I would get errors when trying to persist a collection that perhaps bidirectionality (is that a word?) that would tell me that even though I deleted an object it would be resaved on the Flush. So I'd have to examine how I was trying to persist the collection and would eventually figure out where my 'cascade' skills needed to be improved. While working with the mappings, tho, these options would keep popping up and I couldn't figure out why they were there:

  • unique
  • length
  • index
  • unique-key
  • foreign-key

It was obvious that these were just telling the sql tables about the schema, but I kept wondering if I was supposed to be explicitly stating the foreign key and so on. There just wasn't much in the documentation that dealt with these elements. Finally, I realized these attributes are speaking to the tool hbm2ddl for generating sql schema. Basically, a nifty way of automating the spitting out the schema my entities excpect. The Hibernate in Action book has great detail on how to use these attributes.

I've been missing out on generating my database and have been manually dealing with my tables, so this'll be a big help for dev. Unfortunately, the java tool that compares databases and updates existing schema isn't supported in NHibernate.

One gotcha you need to be aware of, though, is the default to char sql datatype for string C# types. This is a problem Ayende talked about here for discriminator columns. Since the char columns will essentially pad the strings with white space to fill their length, they don't match the discriminator values set in the mapping files. For example, the discriminator column will return "TypeCode   " instead of just "TypeCode" and so you get a exception in NHibernate that is a pain to try to figure out.

Posted on Wednesday, August 30, 2006 5:29 AM O/R Mappers | Back to top


Comments on this post: NHibernate Mapping

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mike Nichols | Powered by: GeeksWithBlogs.net