Try fast search NHibernate

06 June 2009

Auto Quote Table/Column names

Since long time we have a very interesting request on NHibernate JIRA (NH-188).

If you are working in a multi-RDBMS application, you are annoyed, for sure, quoting a table-name or a column-name. As a very good persistent-layer this should be a NHibernate’s work.

I’m happy to announce that the problem is solved (even if, so far, is not done by default).

If you want that NH take the responsibility of properly quote table-name or column-name only where really needed now you can do it in two ways:

  1. Through configuration
  2. Explicitly by code

Through configuration

As you probably know NHibernate’s configuration has some property oriented to mapping-to-DLL tasks.

For schema integration you can use

<property name="hbm2ddl.auto">create-drop</property>

Allowed values for hbm2ddl are:

  • update : auto execute SchemaUpdate on BuildSessionFactory
  • create : auto execute SchemaExport on BuildSessionFactory
  • create-drop : auto execute SchemaExport on BuildSessionFactory recreating the schema
  • validate : auto execute SchemaValidator on BuildSessionFactory

The new property is:

<property name="hbm2ddl.keywords">auto-quote</property>

Allowed values are:

  • none : disable any operation regarding RDBMS KeyWords
  • keywords : (activated by Default)imports all RDBMS KeyWords where the NH-Dialect can provide the implementation of IDataBaseSchema (so far available for MsSQL, Oracle, Firebird, MsSqlCe, MySQL, SQLite, SybaseAnywhere)
  • auto-quote : imports all RDBMS KeyWords and auto-quote all table-names/column-names on BuildSessionFactory

Explicitly by code

When you have an instance of a configured configuration (just before call BuildSessionFactory) you can execute:

SchemaMetadataUpdater.QuoteTableAndColumns(configuration);

That’s all.

The advantage

Take a look to this mapping:

<class name="Order">
<
id type="int">
<
generator class="native"/>
</
id>
<
property name="Select"/>
<
property name="From"/>
<
property name="And"/>
<
property name="Column"/>
<
property name="Name"/>
</
class>

Well… now it is working fine without explicitly quote.

Enjoy NHibernate’s multi-RDBMS easy support.

7 comments:

  1. Seems that setting to anything other than 'none' when using the oracle driver (not the MS one) causes a cast exception with the trace below (NH2.1.2). Using the System.Data.OracleClient seems to work ok.
    > NHibernate.dll!NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare() Line 25 C#
    NHibernate.dll!NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(NHibernate.Dialect.Dialect dialect = {NHibernate.Dialect.Oracle10gDialect}, NHibernate.Tool.hbm2ddl.IConnectionHelper connectionHelper = {NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper}) Line 43 + 0x9 bytes C#
    NHibernate.dll!NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(NHibernate.ISessionFactory sessionFactory = {NHibernate.Impl.SessionFactoryImpl}) Line 17 + 0x24 bytes C#
    NHibernate.dll!NHibernate.Impl.SessionFactoryImpl.SessionFactoryImpl(NHibernate.Cfg.Configuration cfg = {NHibernate.Cfg.Configuration}, NHibernate.Engine.IMapping mapping = {NHibernate.Cfg.Configuration.Mapping}, NHibernate.Cfg.Settings settings = {NHibernate.Cfg.Settings}, NHibernate.Event.EventListeners listeners = {NHibernate.Event.EventListeners}) Line 169 + 0x8 bytes C#
    NHibernate.dll!NHibernate.Cfg.Configuration.BuildSessionFactory() Line 1208 + 0x41 bytes C#

    ReplyDelete
  2. My blog is not exactly the place for NHibernate issues.
    btw have a look to the follow post:
    http://fabiomaulo.blogspot.com/2009/06/from-where-start-to-implements.html

    ReplyDelete
  3. This "hbm2ddl.keywords", "auto-quote" does not work?

    ReplyDelete
  4. I get this error:


    NHibernate.Cfg.HibernateConfigException : An exception occurred parsing configuration :The 'name' attribute is invalid - The value 'hbm2ddl.keywords' is invalid according to its datatype 'String' - The Enumeration constraint failed. ----> System.Xml.Schema.XmlSchemaValidationException : The 'name' attribute is invalid - The value 'hbm2ddl.keywords' is invalid according to its datatype 'String' - The Enumeration constraint failed.


    When adding:

    none

    Any suggestions?

    Thank you for this blog post however, helps me progress!

    ReplyDelete
  5. this is not the place for support request... btw you have not added "none" look at your configuration ;)

    ReplyDelete