Add `backticks` to column names in hibernate


Consider the query

INSERT INTO order (id, key, value) VALUES (?, ?, ?);

There are MySQL syntax errors in the above query as order and key are keywords in MySQL. So the solution would be to add backticks in the query.

INSERT INTO `order` (id, `key`, value) VALUES (?, ?, ?);

Hibernate is dumb enough not to do this automatically. According to this answer by adding the following property to the hibernate configuration file solves the problem with the table name.

<property name="hibernate.globally_quoted_identifiers">true</property>

But this only adds backticks to the database name and the table name. Problem is still there with the column name.

How can I add backticks to all the column names?

  • Hibernate version : 3.6.10.Final
  • MySQL version : 5.7
  • hibernate.dialect : org.hibernate.dialect.MySQL5Dialect
  • JDK version : 1.8 (if that has to do with anything)


I have found a way to add backticks to the column names in hibernate.

  1. Open the xml file (TableName.hbm.xml) relative to the pojo file which has been mapped.
  2. Find the property for the relative column.

    <property name="key" type="string">
        <column name="key" />
  3. Surround the column name with square brackets.

    <property name="key" type="string">
        <column name="[key]" />

But this is a painstaking process as You’ll have to do this to all the columns one by one for all the MySQL keywords. The best way is to avoid adding MySQL keywords as your database, table or column name.

References :

Answered By – Roshana Pitigala

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published