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

查看:111
本文介绍了Hibernate将用户模型保存到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 
@Table(name =\User \)
public class 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"

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将用户模型保存到Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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