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.

Print | posted on Thursday, April 16, 2009 12:47 AM

Feedback


Gravatar

# re: NHibernate mapping - <database-object/> 4/16/2009 9:43 AM Nathan Fisher

Is there any way to specify the type of database the 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.


Gravatar

# re: NHibernate mapping - <database-object/> 4/16/2009 9:52 AM Ayende Rahien

Nathan,
Um, that is what {dialect-scope/} is _for_.


Gravatar

# re: NHibernate mapping - <database-object/> 4/16/2009 2:17 PM Rémy

Damn, this is why I give up on NHibernate’s schema generation capabilities. Didn't know this was possible.

Thanks!


Gravatar

# re: NHibernate mapping - <database-object/> 4/16/2009 2:27 PM Grimace of Despair

I smell a library of attributes and automated database-objects...


Gravatar

# re: NHibernate mapping - <database-object/> 4/21/2009 1:56 PM Nathan Fisher

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


Gravatar

# re: NHibernate mapping - <database-object/> 5/29/2009 2:20 PM Krzysztof Kozmic

When are these executed?

_after_ the NH generated Create or Drop statements?

Can it be used to create the DB itself?


Gravatar

# re: NHibernate mapping - <database-object/> 5/30/2009 1:15 AM Ayende Rahien

Krzysztof,
It is executed as the first thing for drop, last thing for create, IIRC

Comments have been closed on this topic.