Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何配置Hibernate以在表名周围加引号_Hibernate_Postgresql_Grails_Gorm - Fatal编程技术网

如何配置Hibernate以在表名周围加引号

如何配置Hibernate以在表名周围加引号,hibernate,postgresql,grails,gorm,Hibernate,Postgresql,Grails,Gorm,我遇到了一种情况,我试图在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, acc

我遇到了一种情况,我试图在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中指定了应使用PostgreSqlDialogue,但仍然存在这种情况:

dialect = org.hibernate.dialect.PostgreSQLDialect

如何配置Hibernate在处理Postgres时在表名周围加引号?

这个答案可能会帮助您:
它显示了如何在Hibernate和JPA中引用表名。

您可以在
映射
块中使用反勾号引用表名。Hibernate将根据方言将这些转换为数据库的任何引用方法:

static mapping = {
    table "`user`"
}
您也可以将表重命名为不需要引用/转义的其他名称,例如

static mapping = {
    table "users"
}

假设您遵循的是与我相同的教程(“InfoQ提供的Grails入门”),那么您已经了解到它没有为PostgreSQL提供任何内容。基于:

  • “用户”是Postgres中的保留字
  • 映射应该添加到类本身中(这不是绝对明显的):

  • 我强烈建议使用不同的表名。使用保留字会给你带来全身的问题,不仅仅是在Hibernate中。
    class User { 
       String username 
       String password 
       ... 
       static mapping = { 
          table 'users' 
          password column: '`password`' 
       } 
    }
    
    class User { 
       String username 
       String password 
       ... 
       static mapping = { 
          table '`user`' 
          password column: '`password`' 
       } 
    }