views:

188

answers:

2

let's see if I can ask this in an understandable way...

I started with grails and created a domain class called user. As far as I understand, Hibernate is used to map this domain class to the database. This works pretty fine with hsqldb.

Now I tried to switch to javaDB and get an error message because the table is called "user" (which seems to be a reserved word for javaDB).

So a statement like

create table user ...

will result in an error message.

create table "user" ...

works, but Hibernate seems not put put the table name in quotes.

How can I configure Hibernate to use quotes in order to make it work with my table name?

PS: yes, I know, I could map the domain class to another table name... :-)

+1  A: 

Did you try to wrap the table name with double quotes:

class User {
  ..
  static mapping = {
      table '"user"'
  }
}

Update: One of the consequence of this is that you'll have to customize the name of join tables too using the joinTable keyword. This sounds reasonable but conventions are one of the benefits of Grails and not relying on them goes somehow against its philosophy. I would personally just avoid using a reserved word (i.e. not user) here.

Pascal Thivent
doesn't work since hibernate creates other tables which habe this table name in theri name: Unsuccessful: create table "user"_permissions (user_id bigint, permissions_string varchar(255))
Ralf
@Ralf Ah yes, didn't think of that.
Pascal Thivent
A: 

got it. As Pascal already mentioned, the mapping entry is the key. But instead of using double quotes, a back tick will bring success:

static mapping = {
  table "`user"
}

this tells hibernate to put the table name in quotes when generating the sql. It seems also to work with all corresponding tables... but somehow it drops the last character of "user" in some of them... :-(

So it seems that indeed remapping the domain class to a table name which will not cause any problems will be the best way:

static mapping = {
  table "appuser"
}
Ralf