Hibernate将用户模型保存到Postgres [英] Hibernate saving User model to Postgres
问题描述
我通过Hibernate使用Postgres(annotations),但它似乎正在处理一个User对象:
12:09:16,442错误[SchemaExport]失败:create table User(id bigserial not null,password varchar(255),username varchar(255),primary key(id))
12:09:16,442错误[ SchemaExport]错误:语法错误在用户处或附近
如果手动运行SQL,我必须把表名作为用户似乎是一个postgres关键字,但我怎么能说服hibernate做到这一点?
提前致谢。
使用保留关键字时,您需要转义表名。在JPA 1.0中,没有标准化的方法,Hibernate的具体解决方案是使用反引号:
@Entity
@ Table(name =`User`)
public class User {
...
}
在JPA 2.0中,标准化语法如下所示:
@Entity
$ p
@Table(name =\User \)
public class User {
...
}
$引用
- Hibernate核心文档
- JPA 2.0规范
- 2.13数据库对象的命名
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"
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?
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:
@Entity
@Table(name="`User`")
public class User {
...
}
In JPA 2.0, the standardized syntax looks like this:
@Entity
@Table(name="\"User\"")
public class User {
...
}
References
- Hibernate Core documentation
- JPA 2.0 specification
- 2.13 Naming of Database Objects
这篇关于Hibernate将用户模型保存到Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!