本文介绍了如何配置 Hibernate 在表名周围加上引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一种情况,我试图在 Postgres 中创建一个名为user"的表,由于 Hibernate 没有将表名放在引号中,这会引发错误:

I've got a situation where I'm trying to create a table called 'user' in Postgres, which throws an error due to Hibernate not putting table names in quotes:

| 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:

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

dialect = org.hibernate.dialect.PostgreSQLDialect

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

How can I configure Hibernate to put quotes around table names when dealing with Postgres?

推荐答案

您可以在 mapping 块中用反引号引用表名.Hibernate 会将这些转换为基于方言的数据库的任何引用方法:

You can quote the table name in the mapping block with backticks. Hibernate will convert those to whatever the quoting approach is for the database based on the Dialect:

static mapping = {
    table "`user`"
}

您也可以将表重命名为不需要引用/转义的其他名称,例如

You can also just rename the table to something else that doesn't require quoting/escaping, e.g.

static mapping = {
    table "users"
}

这篇关于如何配置 Hibernate 在表名周围加上引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:10