Consider the query
INSERT INTO order (id, key, value) VALUES (?, ?, ?);
There are MySQL syntax errors in the above query as
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.
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.
- Open the xml file (TableName.hbm.xml) relative to the pojo file which has been mapped.
Find the property for the relative column.
<property name="key" type="string"> <column name="key" /> </property>
Surround the column name with square brackets.
<property name="key" type="string"> <column name="[key]" /> </property>
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.
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