Ryan Ricard

www topics worth sharing.

Hibernate Mappings & Oracle Reserved Words

For a project I am currently working on using ColdFusion 9’s ORM Hibernate, I recently found myself in a situation where I wanted to name a table using the Oracle reserved word ‘User’.

I set up my mapping hoping that Hibernate would just take care of it for me…

HBMXML
1
2
3
4
       <class entity-name="User"
              lazy="true"
              name="cfc:UserDirectory.app.model.User"
              table="User">

…oh, if life were just that easy.

ColdFusion kindly returned me this error:

Error while executing the Hibernate query. java.sql.SQLSyntaxErrorException:[Macromedia][Oracle JDBC Driver][Oracle]ORA-00903: invalid table name

Escaping reserved words in Oracle is simple, you just need to wrap the words in the special character grave accent (ASCII Code 96). Easy enough right? Maybe not… when escaping reserved words while mapping Hibernate entities and properties it is important to note… escaped mappings are case sensitive, major gotchya.

So if your table name is ‘USER’, this will not work:

HBMXML
1
2
3
4
  <class entity-name="User"
              lazy="true"
              name="cfc:UserDirectory.app.model.User"
              table="`User`">

Thus, when escaping a mapping when working with Oracle… you also need to ensure the mapping’s case matches the table or column name’s case in your database.

HBMXML
1
2
3
4
  <class entity-name="User"
              lazy="true"
              name="cfc:UserDirectory.app.model.User"
              table="`USER`">

Hopefully this saves somebody some confusion.

Comments