为Hibernate表和列自动保留字转义 [英] Automatic reserved word escaping for Hibernate tables and columns
问题描述
我正在为几个不同的数据库使用一个Hibernate映射:H2,Oracle,MySql。
每个数据库都有不同的保留字列表。 >
我希望Hibernate自动转义保留字。
我知道我可以:
- 使用反引号来强制转义只是为了安全)
- 更改所有标识符,以便它们在任何数据库中肯定不是关键字(使其变丑)
- 将模式绑定到特定一组数据库,转义关键字的联合(如果我添加新的数据库,将会中断)
是否有更优雅的解决方案
AFAIK,Hibernate不保留一个保留关键字(每个数据库)的列表,所以我想你应该看看数据库标识符转义。
如果您使用Hibernate 3.5+,请尝试 hibernate.globally_quoted_identifiers = true
引用所有数据库标识符(这是JPA 2.0中添加的内容,请参阅使用JPA的JPA方法激活此规范的规范的数据库对象 2.13的命名。
在3.5之前,Hibernate doe不提供任何用于全局转义的配置选项。
另请参阅
- HHH-2578 - 重新设计SessionFactory大楼 - 我的理解是,修复这个问题会使自动转义关键字(通过方言)成为可能。
I am trying to use one Hibernate mapping for several different databases: H2, Oracle, MySql.
Each database has a different list of reserved words.
I would like Hibernate to automatically escape the reserved words.
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?
AFAIK, Hibernate doesn't maintain a list of reserved keyword (per database) so I think you should look at database identifier escaping.
If you are using Hibernate 3.5+, try hibernate.globally_quoted_identifiers=true
to quote all database identifiers (this is something they added for JPA 2.0, see the secion 2.13 Naming of Database Objects of the spec for the JPA way to activate this if you are using JPA).
Prior to version 3.5, Hibernate doesn't offer any configuration option for global escaping. Implementing a custom NamingStrategy
to escape everything transparently would be the recommended way.
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.
这篇关于为Hibernate表和列自动保留字转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!