简体   繁体   English

Hibernate将用户模型保存到Postgres

[英]Hibernate saving User model to Postgres

I'm using Postgres via Hibernate (annotations), but it seems to be falling over dealing with a User object: 我正在通过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"

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

Thanks in advance. 提前致谢。

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: 在JPA 1.0中,没有标准化的方法,Hibernate特定的解决方案是使用反引号:

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

In JPA 2.0, the standardized syntax looks like this: 在JPA 2.0中,标准化语法如下所示:

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

References 参考

User is a key word , find a better name or use quotes: "User". 用户是关键词 ,找到更好的名称或使用引号:“用户”。 (bad idea imho, but it works if you do it everywhere ) (坏主意imho,但如果你到处都这样做,它会起作用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM