java Hibernate 表和列的自动保留字转义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3364835/
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
Automatic reserved word escaping for Hibernate tables and columns
提问by Roman Zenka
I am trying to use one Hibernate mapping for several different databases: H2, Oracle, MySql.
我正在尝试对几个不同的数据库使用一种 Hibernate 映射:H2、Oracle、MySql。
Each database has a different list of reserved words.
每个数据库都有不同的保留字列表。
I would like Hibernate to automatically escape the reserved words.
我希望 Hibernate 自动转义保留字。
I know I can:
我知道我可以:
- use backticks to force escaping (escape everything just to be safe)
- change all identifiers so they are certainly not keywords in any database (make them ugly)
- tie the schema to a specific set of databases, escaping the union of keywords (will break if I add new database to the mix)
- 使用反引号强制转义(转义一切只是为了安全)
- 更改所有标识符,因此它们肯定不是任何数据库中的关键字(使它们变得丑陋)
- 将模式绑定到一组特定的数据库,逃避关键字的联合(如果我将新数据库添加到组合中会中断)
Is there a more elegant solution?
有没有更优雅的解决方案?
回答by Pascal Thivent
AFAIK, Hibernate doesn't maintain a list of reserved keyword (per database) so I think you should look at database identifier escaping.
AFAIK,Hibernate 不维护保留关键字列表(每个数据库),所以我认为您应该查看数据库标识符转义。
If you are using Hibernate 3.5+, try hibernate.globally_quoted_identifiers=trueto quote all database identifiers (this is something they added for JPA 2.0, see the secion 2.13 Naming of Database Objectsof the spec for the JPA way to activate this if you are using JPA).
如果您使用的是 Hibernate 3.5+,请尝试hibernate.globally_quoted_identifiers=true引用所有数据库标识符(这是他们为 JPA 2.0 添加的内容,请参阅规范的第2.13 节数据库对象命名以了解在您使用 JPA 时激活它的 JPA 方式)。
Prior to version 3.5, Hibernate doesn't offer any configuration option for global escaping. Implementing a custom NamingStrategyto escape everything transparently would be the recommended way.
在 3.5 版之前,Hibernate 不提供任何用于全局转义的配置选项。实现一个自定义NamingStrategy以透明地转义所有内容将是推荐的方式。
See also
也可以看看
- Database independant Column/Table name escaping?
- HHH-2578 - redesign SessionFactory building- my understanding is that fixing this issue would make automatic escaping of keywords (through dialects) possible.
- 数据库独立的列/表名称转义?
- HHH-2578 - 重新设计 SessionFactory 建筑- 我的理解是解决这个问题将使关键字(通过方言)自动转义成为可能。

![java 带有 ProgressDialog 的外部 AsyncTask 类 [更新:并返回?]](/res/img/loading.gif)