Ayende @ Rahien

Unnatural acts on source code

NHibernate mapping -

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

Nathan Fisher
04/16/2009 06:43 AM by
Nathan Fisher

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.

Ayende Rahien
04/16/2009 06:52 AM by
Ayende Rahien

Nathan,

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

R&#233;my
04/16/2009 11:17 AM by
Rémy

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

Thanks!

Grimace of Despair
04/16/2009 11:27 AM by
Grimace of Despair

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

Nathan Fisher
04/21/2009 10:56 AM by
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

Krzysztof Kozmic
05/29/2009 11:20 AM by
Krzysztof Kozmic

When are these executed?

after the NH generated Create or Drop statements?

Can it be used to create the DB itself?

Ayende Rahien
05/29/2009 10:15 PM by
Ayende Rahien

Krzysztof,

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

Comments have been closed on this topic.