Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic reserved word escaping for Hibernate tables and columns

Tags:

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?

like image 920
Roman Zenka Avatar asked Jul 29 '10 16:07

Roman Zenka


People also ask

How do you escape a reserved word in SQL?

To escape reserved keywords in SQL SELECT statements and in queries on views, enclose them in double quotes ('').

What is reserved word in mysql?

Keywords are words that have significance in SQL. Certain keywords, such as SELECT , DELETE , or BIGINT , are reserved and require special treatment for use as identifiers such as table and column names. This may also be true for the names of built-in functions.


1 Answers

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.
like image 146
Pascal Thivent Avatar answered Sep 21 '22 16:09

Pascal Thivent