Tuesday, 11 May 2010

Random Grails tip: Using a DB reserved word as a domain class name in Grails

We recently came across a situation where we couldn't our Grails app was failing because it was trying to create a table with the name of 'Condition', which turns out to be a reserved word in MySQL... We worked around it by changing the name of the table to 'conditions' by using the Grails ORM DSL, but it turns out there is another way.

Backtick to the rescue...
Hibernate allows you to use backticks (`) to indicate that a name should be escaped - you can simply use this in your grails mapping. For example, we could have used:

class Condition {
String property1
String property2

static mapping = {
table '`condition`'

To be honest, I'm not sure why Grails and/or hibernate don't escape all table and column names by default (I'm sure there is a good reason) - there is an open JIRA issue in Grails around this very problem...


  1. Thank you; elegant solution to my "Order" table.

    I would have thought they could've fixed this already, another JIRA on the watch list!

  2. Percy Jackson 3 is the one movie we believe will stick to the original material we found in Rick Riordan's novel. We believe that the character of Artemis will be well essayed in this last film of the trilogy.
    While the previous films have been high n CGI, the last film will hopefully have more human elements. To know about Percy Jackson 3 Release Date just take a look and you get all the information you need.

  3. This information here will surely be of some help to me : usefull site

  4. Expression of alert. Consider your domain name and how it might be utilized as a part of your business to direct people to your site. This is not to be taken litely.
    Domain register