Because SQL is a declarative language, the keywords that form the grammar of the language are reserved for internal use, and they cannot be employed when defining a database identifier (e.g. catalog, schema, table, column name).
Now, since each relational database provides a custom SQL dialect, the list of reserved keywords may differ from one database to another, as illustrated by the following list:
When persisting the Table entity, the SQL INSERT statement will automatically escape the table and the column names:
INSERT INTO "table" (
'The book table stores book-related info',
Notice that even the id and name column names are escaped this time.
The same applies to any SQL statement generated by Hibernate, so fetching the Table entities matching the provided description generates the following SQL SELECT query:
t."id" AS id1_0_,
t."catalog" AS catalog2_0_,
t."desc" AS desc3_0_,
t."name" AS name4_0_,
t."schema" AS schema5_0_
t."desc" LIKE '%book%'
If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.
Escaping SQL reserved keywords is straightforward when using JPA and Hibernate. While the JPA column-level escaping is very useful if you only have a small number of database identifiers to be escaped, when the number of database identifiers using reserved keywords is large, the Hibernate global escaping becomes a very convenient alternative.
10 000readers have found this blog worth following!
If you subscribeto my newsletter, you'll get:
A free sampleof my Video Course about running Integration tests at warp-speed using Docker and tmpfs
3 chapters from mybook, High-Performance Java Persistence,
Based on my book, High-Performance Java Persistence, this workshop teaches you various data access performance optimizations from JDBC, to JPA, Hibernate and jOOQ for the major rational database systems (e.g. Oracle, SQL Server, MySQL and PostgreSQL).