无法使用名为“user”的表在 postgresql 休眠中

发布于 2024-10-06 03:26:42 字数 119 浏览 4 评论 0原文

当我尝试使用 JPA/hibernate 持久保存一个名为“用户”的实体时,它不起作用。该表未创建,这是因为 user 是 postgresql 中的保留字。除了将表命名为其他名称之外,还有其他方法可以使这项工作正常进行吗?

When I try to persist an entity called "user" with JPA/hibernate it does not work. The table is not created and it is because user is a reserved word in postgresql. Is there any way other than naming the table something else to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

辞取 2024-10-13 03:26:42

要引用标识符,请使用反引号:

@Table(name="`users`")

请参阅 Hibernate 测试套件中的示例:

https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/test/java/org/ hibernate/test/quote/User.java#L31

Hibernate 将自动检测它并转换为适合您正在使用的数据库的引用。

To quote an identifier, use back ticks:

@Table(name="`users`")

See this example from Hibernate's test suite:

https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/test/java/org/hibernate/test/quote/User.java#L31

Hibernate will automatically detect it and convert to the appropriate quote for the database you are using.

风和你 2024-10-13 03:26:42

JPA 支持以下语法来指定必须完全按照指定使用表名:

@Table(name="\"user\"")

尝试在实体类上使用此注释,看看它是否有效。反斜杠用于转义一组双引号,因此看起来有点难看。

JPA supports the following syntax for specifying that the tablename must be used exactly as specified:

@Table(name="\"user\"")

Try using this annotation on your entity class and see if it does the trick. The backslashes are used to escape one set of double-quotes, so it looks kind of ugly.

忆沫 2024-10-13 03:26:42

我想说的是,在 hibernate 中,您应该避免使用保留字的表名。当然你可以逃避它,但它可能会在将来引起问题(例如在查询中)。因此,最安全的方法是以另一种方式命名表 - 例如 users

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

I'd say that you should avoid having table names that are reserved words, with hibernate. Sure you can escape it, but it may cause problems in the future (in a query for example). So the safest way is to name the table another way - say users:

@Entity
@Table(name="users")
public class User {..}
沉溺在你眼里的海 2024-10-13 03:26:42

正如其他人所说,user 是 SQL 和 Postgres 中的保留字。

很多数据库都有很多保留字,我上次统计的有一千多个。因此,由于保留字冲突,很容易遇到奇怪的问题。

尾随下划线:user_

这是我学到的关于 SQL 的最简单的技巧:始终在您的姓名后面添加尾随下划线。我对表名、列名、索引名等执行此操作。

SQL 规范明确承诺 绝不会出现尾随下划线的关键字或保留字。奇怪的是,这个承诺在没有上下文的情况下被插入到规范中。但对我来说,它尖叫着“在你所有的名字后面加上下划线!”。

采用这条规则后,我发现了一个令人愉快的次要好处。当我在代码、评论、问题跟踪和电子邮件中看到下划线时,我总是知道我们特指的是数据库项目,例如 customer_ 表,而不是“ customer”或我的 Java 代码中的类 Customer


不幸的是,我无法引用 SQL 规范,因为它受版权保护。在 SQL:2011 规范中,阅读第 5.4 节名称和标识符标题语法规则第3项,注释111。在SQL-92 请参阅第 5.2 节第 11 项。只需搜索单词 下划线 即可。

As others said, user is a reserved word in SQL and Postgres.

Many databases have many reserved words, over a thousand the last time I tallied. So it is very easy to run into weird problem due to a reserved word collision.

Trailing underscore: user_

Here is the handiest tip I ever learned for SQL: Always append a trailing underscore to your names. I do this for table names, column names, index names, and so on.

The SQL spec specifically promises to never have a keyword or reserved word with a trailing underscore. This promise is oddly inserted into the spec with no context. But to me it screams out “Append underscore to all your names!”.

After adopting this rule, I discovered a pleasant secondary benefit. When I see the underscore in the code, in the comments, in issue-tracking, and in the emails, I always know we are referring specifically to the database item such as customer_ table versus the concept of “customer” or the class Customer in my Java code.


I cannot quote the SQL spec because it is copyright protected, unfortunately. In the SQL:2011 spec, read section 5.4 Names and identifiers under the heading Syntax Rules item 3, NOTE 111. In SQL-92 see section 5.2, item 11. Just searching for the word underscore will work.

迷途知返 2024-10-13 03:26:42

PostgreSQL遵循引用对象名称的ANSI标准,因此您需要指定“user”作为表名(包括双引号)

SELECT *
FROM "user";

我不知道您将如何告诉hibernate生成这样的语句。

我强烈建议您为您的表找到一个不同的名称,这会给您带来更多值得的问题。

PostgreSQL follows the ANSI standard for quoting object names, so you need to specify "user" as the tablename (including the double quotes)

SELECT *
FROM "user";

I don't know how you would tell hibernate to generate such a statement.

I strongly recommend you find a different name for your table, it will give you more problems that it's worth.

挽袖吟 2024-10-13 03:26:42

您可以使用模式名称来引用用户表。如果您不使用任何特定的公共架构,请使用默认的公共架构。

@Table(name="user", schema="public")

You can use schema name to refer to the user table. Use default public schema if you aren't using any specific one.

@Table(name="user", schema="public")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文