Mapping by code
XML files have been NHibernate's default approach to mapping since its inception. It's a platform neutral, flexible and easily parsed format. The mapping syntax for Java's Hibernate is virtually identical to NHibernate's.
NHibernate also allows you to specify the mappings using nothing but code. This gives us a couple of advantages, since the mapping code gets intricately connected to the classes it should map. There is no risk of misspelled class or property names and you can use runtime logic to customize the mappings.
Getting ready
Complete the Getting ready instructions given at the beginning of this chapter.
The recipe uses the entity classes that we created in Eg.Core
in the preceding recipes of this chapter. However, for convenience, NH4CookbookHelpers
also provide the same class model and we will use that model here. Feel free to modify the code (changing the using
statements) to use Eg.Core
, if that suits you better. You may have to add a version
property (integer) to...