NHibernate Pitfalls: XML Mappings
This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
If you are still using XML mappings – .hbm.xml files –, there’s nothing wrong with that, but you may run into problems.
First, you will need to add these files as embedded resources in your project, if you are calling Configuration.AddAssembly(), Configuration.AddClass() or you are specifying the name of the assembly in the .config file like this:
1: <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
2: <session-factory>
3: <property name="connection.driver_class">NHibernate.Driver.Sql2008ClientDriver</property>
4: <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
5: <property name="connection.connection_string_name">MyConnection</property>
6: <mapping assembly="MyAssembly.MyModel" />
7: </session-factory>
8: </hibernate-configuration>
These methods will look for .hbm.xml files as embedded resources in that assembly:
Alternatively, you can have the files on the filesystem, but you will have to call Configuration.AddDirectory(), Configuration.AddFile() or Configuration.AddInputStream(), passing the appropriate parameters.
In either case, the name of each .hbm.xml must end with .hbm.xml (of course!) and must be composed of the name of the associated class including its namespace.
If you don’t do this, NHibernate will not find your mappings, which will result in runtime errors.