18

I am faced with legacy system written to work with MySQL 5.0 and now need to migrate it to MysQL 5.5 (requirement). I found that one column was named maxvalue, which seems to be system word in MySQL 5.5. Thus all my Hibernate queries that include this column give syntax error:

Caused by: java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'maxvalue

It seems that Hibernate does not automatically but backquotes ` around the field name. If I extract the query, backquote `maxvalue` it runs correctly in MySQL 5.5.

I have found solution how to explicitly force backquotes for specific field / table. The thing is that I am not sure how many other column names will produce such problem. Is there a way to tell Hibernate to automatically backquote all table/ column names? (which will produce valid SQL and I don't know why it does not do that by default for MySQL).

EDIT: This discussion almost makes me believe that what I want is not possible.

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135

1 Answers1

41

There is a non-documented property for this purpose. Use,

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

or

<property name="hibernate.globally_quoted_identifiers">true</property>
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • Perfect! Thanks! Now this property is documented at least somewhere. regretfully I am running hibernate 3.3 and it seems this property is introduced in 3.5. I will not be able to validate this property is actually working, but I am marking your answer as accepted. – Boris Strandjev Feb 19 '13 at 09:22
  • 9
    for people using Spring: the application.properties prop is: 'spring.jpa.properties.hibernate.globally_quoted_identifiers=true' – pvgoddijn Dec 11 '15 at 09:34
  • "There's no such thing as an undocumented feature. If your users don't know about a feature, its a nonfeature. Get rid of it; it's just complicating the sourcecode." -- https://developer.jboss.org/wiki/WhyThisProjectIsSuccessful – Sebastian Kirsche Jun 21 '16 at 11:23
  • 2
    This only add backticks to the table names and db name. Columns are still the same... `insert into 'default_settings' (account, key, value) values (?, ?, ?)`. Problem is with the column name `key`. – Roshana Pitigala Dec 16 '17 at 09:38