hibernate - 如何配置 Hibernate 在表名周围加上引号

标签 hibernate postgresql grails grails-orm

我有这样一种情况,我试图在 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/

相关文章:

java - Hibernate 映射类实体名称

asp.net-mvc - 使用现有 Postgres 数据库访问 MVC3 数据库

unit-testing - Grails mockFor 以及如何最好地测试使用正确参数调用的方法

arrays - Grails中的全局数组?

hibernate - Hibernate 是否支持嵌套事务?

java - 编写 orm.xml 的最佳实践

java - 如何使用 ReST Web 服务的 spring 验证来验证输入模型并返回适当的错误消息

PostgreSQL 使用复制功能后无法进行简单查询

bash - 使用 bash 脚本删除 postgresql 中的所有表

grails - .link_to_grails_plugins 文件夹的用途是什么?