Automatic reserved word escaping for Hibernate tables and columns
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.
How to map an entity field whose name is a reserved word in JPA
Had the same problem, but with a tablename called Transaction
. If you set
hibernate.globally_quoted_identifiers=true
Then all database identifiers will be quoted.
Found my answer here
Special character in table name hibernate giving error
And found all available settings here
https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/appendices/Configurations.html
Could not find better docs for this though.
In my case the setting was in my Spring properties file. As mentioned in the comments, it could also be in other, hibernate related, configuration files.
hibernate inserting without left ticks
Quote the table/ column names you need quoted with ` back-ticks in your .hbm.xml or mapping annotations.
For example:
<property name='DB" column='`db`' />
@Column(name="`db`")
This tells Hibernate to quote these identifiers to the database. I use this, for example, with a `USER` table. As with any app, most table & column-names are not conflicting but user
tends to be a SQL keyword.
You don't need to switch on quoting globally.
Hibernate throws SQLGrammarException
You have a column named desc
, and desc
is a reserved work in MySql (and many other databases).
You can:
- From this and this you can make hibernate escape all column and table names:
hibernate.globally_quoted_identifiers=true
(in yourpersistence.xml
, or hibernate configuration. - Change the name of the column
- From this you can escape only this identifier using:
@Column(name="\"desc\"")
Also, if is not a legacy database, please consider renaming.
SQL Error: 1064 Hibernate
CHARACTER
is a reserved word. If you're going to name SQL objects with such words, then you must ensure that they are appropriately quoted.
Adapting Pascal Thivent's excellent answer to Creating field with reserved word name with JPA:
With Hibernate as JPA 1.0 provider, you can escape a reserved keyword by enclosing it within backticks:
@Table(name = "`character`")
This is the syntax inherited from Hiberate Core:
5.4. SQL quoted identifiers
You can force Hibernate to quote an
identifier in the generated SQL by
enclosing the table or column name in
backticks in the mapping document.
Hibernate will use the correct
quotation style for the SQL Dialect.
This is usually double quotes, but the
SQL Server uses brackets and MySQL
uses backticks.<class name="LineItem" table="`Line Item`">
<id name="id" column="`Item Id`"/><generator class="assigned"/></id>
<property name="itemNumber" column="`Item #`"/>
...
</class>
In JPA 2.0, the syntax is standardized and becomes:
@Table(name="\"character\"")
References
- Hibernate reference guide
- 5.4. SQL quoted identifiers
- JPA 2.0 specification
- 2.13 Naming of Database Objects
Related questions
- Hibernate, MySQL and table named “Repeat” - strange behaviour
- Automatic reserved word escaping for Hibernate tables and columns
Why I cannot define an column named index in jpa?
I haven't tried using mysql, but 'index' seems to be a reserved word.
check this out.
Related Topics
Java Implementation of JSON to Xml Conversion
What Are the Rules Dictating the Inheritance of Static Variables in Java
How to Attach Multiple Files to an Email Using Javamail
How to Find Out If "Debug Mode" Is Enabled
How to Quickly Determine If a Method Is Overridden in Java
How to Find Out If the Value Contained in a String Is Double or Not
Could Not Set the Column Width to Zero I.E. Not Made Column Invisible
How to Unit Test Abstract Classes: Extend with Stubs
Java 8 Default Methods as Traits:Safe
Try with Resources Introduce Unreachable Bytecode
Count Occurrences of Words in Arraylist
Reverse Java Graphics2D Scaled and Rotated Coordinates
How to Use Sqoop in Java Program
How to Use 3Rd Party Library in Java9 Module
Difference Between Paint() and Paintcomponent()