How to configure Hibernate to place quotes around table names

I have a situation where I try to create a table named "user" in Postgres that throws an error due to the fact that Hibernate does not put the table names in quotation marks:

| Error 2012-02-27 23:06:58,782 [Thread-10] ERROR hbm2ddl.SchemaExport - Unsuccessful: create table user (id int8 not null, version int8 not null, account_expired bool not null, account_locked bool not null, email_address varchar(255) not null, enabled bool not null, first_name varchar(255) not null, last_name varchar(255) not null, mobile_number varchar(255) not null, "password" varchar(255) not null, password_expired bool not null, username varchar(255) not null unique, primary key (id)) 

This is despite the fact that it should use PostgreSQLDialect in DataSource.groovy:

 dialect = org.hibernate.dialect.PostgreSQLDialect 

How to configure Hibernate to place quotes around table names when working with Postgres?

+4
source share
3 answers

You can specify the table name in the mapping block with reverse windows. Hibernate converts them to any dialect-based database quinging approach:

 static mapping = { table "`user`" } 

You can also just rename the table to another, which does not require quoting / escaping, for example.

 static mapping = { table "users" } 
+7
source

This answer may help you: fooobar.com/questions/365156 / ... It shows how to specify table names in Hibernate and JPA.

+4
source

Assuming you follow the same tutorial as me (β€œGetting Started with Grails” from InfoQ), you already realized that it does not provide any PostgreSQL coverage. Based on this post :

  • "User" is a reserved word in Postgres.
  • The mapping should be added to the class itself (this is not entirely obvious):
 class User { String username String password ... static mapping = { table 'users' password column: '`password`' } } 

or

 class User { String username String password ... static mapping = { table '`user`' password column: '`password`' } } 
+1
source

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


All Articles