Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate: "Column 'Reserved Word' does not belong to table ReservedWords." error

Tags:

nhibernate

I just upgraded to NHibernate 2.1 from 2.0 and w/o changing my schema now I get the error:

Column 'Reserved Word' does not belong to table ReservedWords.

when trying to .OpenSession().

I can add the property:

<property name="hbm2ddl.keywords">none</property>

to the hibernate.cfg.xml file, which "fixes" the error.

However, it would be helpful to know, why this error is occurring with the upgrade and how I might fix it.

like image 665
ChrisP Avatar asked Jul 28 '09 18:07

ChrisP


1 Answers

Taken from http://fabiomaulo.blogspot.com/2009/06/auto-quote-tablecolumn-names.html

NHibernate added the ability to automatically quote table and column names that are reserved words for the selected database.

For example if you have a class with a property "Order", if you try to do: SELECT Order FROM MyTable , then most database will fail to parse the query. In MSSQL, you would have to do SELECT [Order] FROM MyTable to get it to work (hence the quoting of fields).

Generically in NHibernate you can quote fields using `backticks` - ie. in your mapping specifying the column for Order as Order would then allow you to use a column called Order.

The change in NHibernate allows the fields that need to be quote to be quoted automatically, so you don't need to add the quotes manually.

Taken from that url, for example 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>

Would work without having to quote any of the table or column names

like image 109
Martin Ernst Avatar answered Oct 05 '22 23:10

Martin Ernst