Java 使用 JPA 创建带有保留字名称的字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2224503/
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
Creating field with reserved word name with JPA
提问by TJR
@Column(name="open")
Using sqlserver dialect with hibernate.
在休眠状态下使用 sqlserver 方言。
[SchemaUpdate] Unsuccessful: create table auth_session (id numeric(19,0) identity not null, active tinyint null, creation_date datetime not null, last_modified datetime not null, maxidle int null, maxlive int null, open tinyint null, sessionid varchar(255) not null, user_id numeric(19,0) not null, primary key (id), unique (sessionid))
[SchemaUpdate] Incorrect syntax near the keyword 'open'.
I would have expected hibernate to use quoted identifier when creating the table.
我希望 hibernate 在创建表时使用带引号的标识符。
Any ideas on how to handle this... other than renaming the field?
关于如何处理这个问题的任何想法......除了重命名字段?
采纳答案by Rafiek
Had the same problem, but with a tablename called Transaction
. If you set
有同样的问题,但有一个名为Transaction
. 如果你设置
hibernate.globally_quoted_identifiers=true
Then all database identifiers will be quoted.
然后将引用所有数据库标识符。
Found my answer here Special character in table name hibernate giving error
在这里找到我的答案 表名中的特殊字符休眠给出错误
And found all available settings here https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/appendices/Configurations.html
并在这里找到所有可用的设置 https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/appendices/Configurations.html
Could not find better docs for this though.
尽管如此,找不到更好的文档。
In my case the setting was in my Spring properties file. As mentioned in the comments, it could also be in other, hibernate related, configuration files.
在我的情况下,设置在我的 Spring 属性文件中。正如评论中提到的,它也可能在其他与休眠相关的配置文件中。
回答by Bozho
No - change the column name.
否 - 更改列名称。
This is database-specific, and you just can't create such a column. After all hibernate finally sends DDL to the database. If you can't create a valid DDL with this column name, this means hibernate can't as well. I don't think quoting would solve the issue even if you are writing the DDL.
这是特定于数据库的,您不能创建这样的列。毕竟 hibernate 最终将 DDL 发送到数据库。如果你不能用这个列名创建一个有效的 DDL,这意味着 hibernate 也不能。我认为即使您正在编写 DDL,引用也不能解决问题。
Even if you somehow succeed to escape the name - change it. It will work with this database, but won't work with another.
即使您以某种方式成功逃脱了名称 - 更改它。它将与这个数据库一起工作,但不能与另一个数据库一起工作。
回答by Raman
If you use as shown below it should work
如果您按如下所示使用它应该可以工作
@Column(name="[order]")
private int order;
回答by Pascal Thivent
With Hibernate as JPA 1.0 provider, you can escape a reserved keyword by enclosing it within backticks:
使用 Hibernate 作为 JPA 1.0 提供程序,您可以通过将保留关键字括在反引号中来转义保留关键字:
@Column(name="`open`")
This is the syntax inherited from Hiberate Core:
这是从 Hiberate Core 继承的语法:
5.4. SQL quoted identifiers
You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. Hibernate will use the correct quotation style for the SQL Dialect. This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.
<class name="LineItem" table="`Line Item`"> <id name="id" column="`Item Id`"/><generator class="assigned"/></id> <property name="itemNumber" column="`Item #`"/> ... </class>
5.4. SQL 引用标识符
您可以通过将表名或列名括在映射文档中的反引号中来强制 Hibernate 在生成的 SQL 中引用标识符。Hibernate 将为 SQL 方言使用正确的引用样式。这通常是双引号,但 SQL Server 使用方括号,而 MySQL 使用反引号。
<class name="LineItem" table="`Line Item`"> <id name="id" column="`Item Id`"/><generator class="assigned"/></id> <property name="itemNumber" column="`Item #`"/> ... </class>
In JPA 2.0, the syntax is standardized and becomes:
在 JPA 2.0 中,语法被标准化并变为:
@Column(name="\"open\"")
References
参考
- Hibernate reference guide
- JPA 2.0 specification
- 2.13 Naming of Database Objects
- 休眠参考指南
- JPA 2.0 规范
- 2.13 数据库对象的命名
Related questions
相关问题
回答by Neil Stockton
Some JPA implementations (e.g the one I use, DataNucleus) automatically quote the identifier for you, so you never get this.
一些 JPA 实现(例如我使用的那个,DataNucleus)会自动为你引用标识符,所以你永远不会得到这个。
回答by wmnitin
@Column(name="\"open\"")
This will work for sure, Same problem happened with me, when I was learning hibernate.
这肯定会起作用,当我学习休眠时,我也遇到了同样的问题。
回答by Vlad Mihalcea
Manually escaping the reserved keywords
手动转义保留关键字
If you are using JPA, you can escape with double quotes:
如果您使用的是 JPA,则可以使用双引号进行转义:
@Column(name = "\"open\"")
If you're using Hibernate native API, then you can escape them using backticks:
如果您使用的是 Hibernate 本机 API,那么您可以使用反引号将它们转义:
@Column(name = "`open`")
Automatically escaping reserved keywords
自动转义保留关键字
If you want to automatically escape reserved keywords, you can set to true
the Hibernate-specific hibernate.globally_quoted_identifiers
configuration property:
如果要自动转义保留关键字,可以设置为true
Hibernate 特定的hibernate.globally_quoted_identifiers
配置属性:
<property
name="hibernate.globally_quoted_identifiers"
value="true"
/>
For more details, check out this article.
有关更多详细信息,请查看这篇文章。