为Hibernate表和列自动保留字转义 [英] Automatic reserved word escaping for Hibernate tables and columns

查看:552
本文介绍了为Hibernate表和列自动保留字转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为几个不同的数据库使用一个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不提供任何用于全局转义的配置选项。

另请参阅




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

这篇关于为Hibernate表和列自动保留字转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆