How to Map an Entity Field Whose Name Is a Reserved Word in JPA

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.

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.

Sql keyword usage as a jpa entity column name

According to the hibernate documentation:

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. While traditionally, Hibernate used backticks for escaping SQL reserved keywords, JPA uses double quotes instead.

So, you can correct your mapping in this way:

@Column(name = "`default`")
private boolean isDefault;

or this way:

@Column(name = "\"default\"")
private boolean isDefault;

JPA reserved keywords to postgres

Use:

@Table(name = "`user`")

and it works...

Using reserved JPQL keywords with JPA

Will this name be escaped?

There is nothing in the JPA spec that says so, if your provider does, this is provider specific.

Would the use of a different table name solve the problem @Table(name="otherName")

Obviously, it would (as long as you don't use another reserved keyword of course). But if you are using a JPA 2.0 provider, there is a standard way to get a db object name escaped, with double quotes:

@Table(name="\"Group\"")

In JPA 1.0, there is nothing standard, it depends on your JPA provider. For example, Hibernate uses backticks:

@Table(name="`Group`")

Or should I rename the class?

No. The table name of an entity defaults to the entity name but you can control it using the @Table annotation as we saw. There is thus no need to change the class name of your entity.

how to map boolean fields in model class through hibernate annotations

In fact your problem is that you used Like as a column name in your entity, so Hibernate will try to map this column to the name Like while it's a reserved keyword in SQL, that's why you got the exception:

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 'Like bit' at line 2

What you can do here is either to use a different name of the column or escape the name.

For further details and escaping solutions you can check:

  • Hibernate Tips: How to escape table and column names article.
  • Creating field with reserved word name with JPA

Spring-Data-Jpa Repository - Underscore on Entity Column Name

The underscore _ is a reserved character in Spring Data query derivation (see the reference docs for details) to potentially allow manual property path description. So there are two options you have:

  1. Stick to the Java naming conventions of using camel-case for member variable names and everything will work as expected.
  2. Escape the _ by using an additional underscore, i.e. rename your query method to findByMunicipal__idOrderByLastnameDesc(…).

I'd recommend the former as you're not going to alienate fellow Java developers :).

Spring boot JPA insert in TABLE with uppercase name with Hibernate

On hibernate 5, it would be

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

in your application.properties file.



Related Topics



Leave a reply



Submit