1

I just installed Grails 3.2.0.M2 and created a new domain class named Group. I then ran the generate-all * command and attempted to browsed to the GroupController, but was greeted with this error:

URI:       /group/index
Class:     org.h2.jdbc.JdbcSQLException
Message:   null
Caused by: Syntax error in SQL statement "SELECT COUNT(*) AS Y0_ FROM GROUP[*] THIS_ "; expected "identifier"; SQL statement: select count(*) as y0_ from group this_ [42001-192]

Which is happening here:

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond Group.list(params), model:[groupCount: Group.count()] // Error occurs here
}

The weird thing is that the problem goes away if I rename the domain class and controller to Groupz and GroupzController respectively. Why can't I name my domain class group? What other names are illegal for domain classes?

ubiquibacon
  • 10,451
  • 28
  • 109
  • 179

2 Answers2

2

You can find a list of reserved words on the Grails wiki.

Alternatively, you can get around this issue by using the the static mapping property to rename your class:

class Group {
    ...
    static mapping = { table 'my_group' }
    ...
}
seth.miller
  • 1,988
  • 17
  • 23
  • 1
    You can also continue to use the reserved word but enclose it in backtick characters; this will get Hibernate to use the correct quoting/escaping for that database, as seen in [this older answer](http://stackoverflow.com/a/9468002/160313). – Burt Beckwith Jul 23 '16 at 20:23
0

Well all the domain classes are scanned and converted to the names in following way

User -> user UserMapping -> user_mapping and so on.

Basicall the camel case is String is split by the upper case character and then separated by _.

You cannot use any Domain class name that results in the >restricted/reserved words in sql.

e.q group, select, where, from, join, etc

if you still want to use these names in Grails then you can change the table name using mapping block. Grails will refer to your domain/entity as the class name but internally bind it to the mapping specified

e.g

static mapping {
   table : 'my_table_name'
}
Community
  • 1
  • 1
Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23