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

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

问题描述

我正在尝试为几个不同的数据库使用一个 Hibernate 映射:H2、Oracle、MySql.

I am trying to use one Hibernate mapping for several different databases: H2, Oracle, MySql.

每个数据库都有不同的保留字列表.

Each database has a different list of reserved words.

我希望 Hibernate 自动转义保留字.

I would like Hibernate to automatically escape the reserved words.

我知道我可以:

  • 使用反引号强制转义(转义一切只是为了安全)
  • 更改所有标识符,使它们肯定不是任何数据库中的关键字(使它们变得丑陋)
  • 将模式绑定到一组特定的数据库,逃避关键字的联合(如果我添加新的数据库会中断)

有没有更优雅的解决方案?

Is there a more elegant solution?

推荐答案

AFAIK,Hibernate 不维护保留关键字列表(每个数据库)所以我认为你应该看看数据库标识符转义.

AFAIK, Hibernate doesn't maintain a list of reserved keyword (per database) so I think you should look at database identifier escaping.

如果您使用的是 Hibernate 3.5+,请尝试 hibernate.globally_quoted_identifiers=true 引用所有数据库标识符(这是他们为 JPA 2.0 添加的内容,请参阅 2.13 数据库对象命名一节 的 JPA 方式的规范,如果您使用 JPA,则可以激活它).

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

在 3.5 版之前,Hibernate 不提供任何用于全局转义的配置选项.推荐的方法是实现自定义 NamingStrategy 以透明地转义所有内容.

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.

  • 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屋!

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