User table with javaDB and Hibernate from inside grails

see if I can ask about this in an understandable way ...

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

Now I tried switching to javaDB and getting 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 doesn't seem to put quotes around the table name.

How do I configure Hibernate to use quotes to make it work with my table name?

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

+4
source share
2 answers

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

 static mapping = { table "`user" } 

this says hibernate quotes the table name when generating sql. It seems that it works with all the relevant tables ... but somehow it misses the last β€œuser” character in some of them ... :-(

So, it seems that the best way to reassign a domain class to a table name that will not cause any problems would be best:

 static mapping = { table "appuser" } 
0
source

You tried to wrap the table name with double quotes:

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

Update:. One consequence of this is that you also have to customize the name of the join tables using joinTable . It sounds reasonable, but conventions are one of Grails' benefits, and not relying on them is somehow contrary to its philosophy. I personally simply would not use the reserved word (i.e. Not user ) here.

+1
source

Source: https://habr.com/ru/post/1307114/


All Articles