【问题标题】:Getting error creating tables with ImprovedNamingStrategy使用改进命名策略创建表时出错
【发布时间】:2012-12-03 17:34:20
【问题描述】:

我的 pojo 看起来像这样:

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

当我将名称保留在那里时,一切正常,休眠 sql 日志:

 create table users (id int8 not null, username varchar(255), primary key (id))

当我删除注释时,我得到:

Hibernate: create table user (id int8 not null, username varchar(255), primary key (id))
19:24:43 [localhost-startStop-23] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: create table user (id int8 not null, username varchar(255), primary key (id))
19:24:43 [localhost-startStop-23] ERROR o.h.tool.hbm2ddl.SchemaExport - ERROR: syntax error at or near "user" Position: 14

我正在尝试使用它,因为它在我的 xml 中定义:

<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />

任何建议,为什么命名策略不起作用?

【问题讨论】:

  • 用户不是保留关键字吗?

标签: java spring hibernate postgresql spring-mvc


【解决方案1】:

默认表名USER 是保留关键字。在这种情况下,您必须明确指定 name 才能为其添加必要的转义:

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

【讨论】:

  • 使用该设置,表名会是什么?会是带有引号的 USER 吗?
  • @Jaanus: USER 不带引号。引号告诉 Hibernate 在查询中应用必要的转义。
  • 你确定它有效吗,其他张贴者说,根本不允许该用户使用表名。或者 CAPS 版本可以工作?
  • 大小写无关紧要。文档说:“作为一般规则,如果您对包含任何列出的关键字作为标识符的命令产生虚假的解析器错误,您应该尝试引用标识符以查看问题是否消失。”如果在查询中引用,user 是一个有效的表名。
  • 所以其他答案基本上是错误的,我可以在PostgreSQL中创建名为user的表。
【解决方案2】:

PostgreSQL 关键字列表:

http://www.postgresql.org/docs/9.1/static/sql-keywords-appendix.html

标记为“保留”的是那些不允许作为列或 表名。

还有你的表名:

用户保留

实际上是保留的。

所以你不能在 PostgreSQL 中创建名为 user 的表。这就是为什么使用注释一切正常,因为注释指定名称users。没有这样的关键字。

【讨论】:

    猜你喜欢
    • 2017-01-02
    • 2017-11-02
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2019-09-14
    • 2020-10-30
    • 2021-10-16
    相关资源
    最近更新 更多