Hibernate将用户模型保存到Postgres

ric*_*ich 18 java postgresql orm hibernate

我正在通过Hibernate(注释)使用Postgres,但它似乎在处理User对象时失败了:

12:09:16,442 ERROR [SchemaExport] Unsuccessful: create table User (id  bigserial not null, password varchar(255), username varchar(255), primary key (id))
12:09:16,442 ERROR [SchemaExport] ERROR: syntax error at or near "User"
Run Code Online (Sandbox Code Playgroud)

如果我手动运行SQL,我必须在表名周围加上引号,因为用户似乎是一个postgres关键字,但我怎么能说服hibernate自己做呢?

提前致谢.

Pas*_*ent 39

使用保留关键字时,您需要转义表名.在JPA 1.0中,没有标准化的方法,Hibernate特定的解决方案是使用反引号:

@Entity
@Table(name="`User`")
public class User {
    ...
}
Run Code Online (Sandbox Code Playgroud)

在JPA 2.0中,标准化语法如下所示:

@Entity
@Table(name="\"User\"")
public class User {
    ...
}
Run Code Online (Sandbox Code Playgroud)

参考

  • @ Mr.Omsn:默认情况下引用是关闭的,因为要将JPA查询语言转换为纯SQL.在SQL标识符中,例如表名和列名都是__in __敏感的.所以SQL会认为`user`和`User`是同一个表.__BUT__"用户"和"用户"将是不同的表,它们也可以同时存在.PostgreSQL和SQL标准之间的唯一区别是:PG折叠为小写,但SQL折叠为大写. (2认同)