Hibernate 将用户模型保存到 Postgres [英] Hibernate saving User model to Postgres

查看:20
本文介绍了Hibernate 将用户模型保存到 Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Postgres via Hibernate (annotations), but it seems to be falling over dealing with a User object:

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"

如果我手动运行 SQL,我必须在表名周围加上引号,因为 user 似乎是 postgres 关键字,但是我如何说服 hibernate 自己执行此操作?

If I run the SQL manually I have to put quotes around the table name as user seems to be a postgres keyword, but how can I convince hibernate to do this itself?

提前致谢.

推荐答案

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

You need to escape the table name when using reserved keywords. In JPA 1.0, there is no standardized way and the Hibernate specific solution is to use backticks:

@Entity
@Table(name="`User`")
public class User {
    ...
}

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

In JPA 2.0, the standardized syntax looks like this:

@Entity
@Table(name=""User"")
public class User {
    ...
}

参考资料

  • Hibernate 核心文档
    • 5.4.SQL 引用标识符
      • 2.13 数据库对象的命名

      这篇关于Hibernate 将用户模型保存到 Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆