我有这样一种情况,我试图在 Postgres 中创建一个名为“user”的表,由于 Hibernate 没有将表名放在引号中而引发错误:
| 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))
尽管指定它应该在 DataSource.groovy 中使用 PostgreSQLDialect:
dialect = org.hibernate.dialect.PostgreSQLDialect
在处理 Postgres 时,如何配置 Hibernate 在表名周围加上引号?
最佳答案
您可以在 mapping
block 中用反引号引用表名。 Hibernate 会将这些转换为基于方言的数据库的任何引用方法:
static mapping = {
table "`user`"
}
您也可以将表格重命名为不需要引用/转义的其他名称,例如
static mapping = {
table "users"
}
关于hibernate - 如何配置 Hibernate 在表名周围加上引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9463240/