NHibernate mapping - <database-object/>
I, like many, have grown used to NHibernate’s schema generation capabilities. Those make working with databases such a pleasure that I cannot imagine trying without them.
However, at some point, even NHibernate’s smarts reach an end, and such an occasion requires the use of direct SQL to manipulate the database directly. A good example of that would be:
<!-- SQL Server need this index --> <database-object> <create> CREATE INDEX PeopleByCityAndLastName ... </create> <drop> DROP INDEX PeopleByCityAndLastName </drop> <dialect-scope name="NHibernate.Dialect.MsSql2000Dialect"/> <dialect-scope name="NHibernate.Dialect.MsSql2005Dialect"/> <dialect-scope name="NHibernate.Dialect.MsSql2008Dialect"/> </database-object> <!-- Oracle need this stats only --> <database-object> <create> CREATE STATISTICS PeopleByCityAndLastName ... </create> <drop> DROP STATISTICS PeopleByCityAndLastName </drop> <dialect-scope name="NHibernate.Dialect.OracleDialect"/> <dialect-scope name="NHibernate.Dialect.Oracle9Dialect"/> </database-object>
As you can see, this allows us to execute database specific SQL, using the dialect scope. It is not a common feature, but it can be incredibly useful.
Comments
Is there any way to specify the type of database the <database-object can be run against? This would allow you have multiple dialects within the mapping.
case in point. I'm developing against SS2005 and MySQL5.0 and it would be great to specify the dialect as the SQL needed is slightly different, as you have shown in you artical above. This would mean that I wont need to keep commented out mappings info depending on the database I ned to point it at.
Nathan,
Um, that is what {dialect-scope/} is _for_.
Damn, this is why I give up on NHibernate’s schema generation capabilities. Didn't know this was possible.
Thanks!
I smell a library of attributes and automated database-objects...
Ayende,
thankls for the blinding flash of the obvious.. :). I had scimmed over the blog and failed to notice that it was all about what I was looking for.
Time to wake up and go and get some more cofffee.
Nathan
When are these executed?
after the NH generated Create or Drop statements?
Can it be used to create the DB itself?
Krzysztof,
It is executed as the first thing for drop, last thing for create, IIRC
Comment preview