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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4350874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:25:53  来源:igfitidea点击:

Unable to use table named "user" in postgresql hibernate

hibernatepostgresqljpa

提问by Piotr

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?

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

采纳答案by jpkrohling

To quote an identifier, use back ticks:

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

@Table(name="`users`")

See this example from Hibernate's test suite:

从 Hibernate 的测试套件中查看此示例:

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

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.

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

回答by Jim Tough

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

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

@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.

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

回答by Bozho

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:

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

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

回答by a_horse_with_no_name

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

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

SELECT *
FROM "user";

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

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

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

我强烈建议你为你的表找一个不同的名字,它会给你带来更多值得的问题。

回答by Basil Bourque

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

正如其他人所说,user是 SQL 和 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_

尾随下划线: user_

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

这是我学到的关于 SQL 的最方便的技巧:总是在你的名字后面附加一个下划线。我对表名、列名、索引名等执行此操作。

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!”.

SQL 规范特别承诺永远不要有带有尾随下划线的关键字或保留字。这个承诺被奇怪地插入到没有上下文的规范中。但对我来说,它尖叫着“在你所有的名字后面加上下划线!”。

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 Customerin my Java code.

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



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

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