How to map an entity field whose name is a reserved word in JPA

JavaSql ServerHibernateOrmJpa

Java Problem Overview


@Column(name="open")

Using sqlserver dialect with hibernate.

[SchemaUpdate] Unsuccessful: create table auth_session (id numeric(19,0) identity not null, active tinyint null, creation_date datetime not null, last_modified datetime not null, maxidle int null, maxlive int null, open tinyint null, sessionid varchar(255) not null, user_id numeric(19,0) not null, primary key (id), unique (sessionid))
[SchemaUpdate] Incorrect syntax near the keyword 'open'.

I would have expected hibernate to use quoted identifier when creating the table.

Any ideas on how to handle this... other than renaming the field?

Java Solutions


Solution 1 - Java

With Hibernate as JPA 1.0 provider, you can escape a reserved keyword by enclosing it within backticks:

@Column(name="`open`")

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. > > > > > ... >

In JPA 2.0, the syntax is standardized and becomes:

@Column(name="\"open\"")
References

Solution 2 - Java

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 https://stackoverflow.com/questions/34065136/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.

Solution 3 - Java

Manually escaping the reserved keywords

If you are using JPA, you can escape with double quotes:

@Column(name = "\"open\"")

If you're using Hibernate native API, then you can escape them using backticks:

@Column(name = "`open`")

Automatically escaping reserved keywords

If you want to automatically escape reserved keywords, you can set to true the Hibernate-specific hibernate.globally_quoted_identifiers configuration property:

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

Yaml format

spring:
  jpa:
    properties:
      hibernate:
        globally_quoted_identifiers: true

Solution 4 - Java

If you use as shown below it should work

@Column(name="[order]")
private int order;

Solution 5 - Java

@Column(name="\"open\"")

This will work for sure, Same problem happened with me, when I was learning hibernate.

Solution 6 - Java

There is also another option: hibernate.auto_quote_keyword

which >Specifies whether to automatically quote any names that are deemed keywords.

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

Yaml

spring:
  jpa:
    properties:
      hibernate:
        auto_quote_keyword: true

Solution 7 - Java

No - change the column name.

This is database-specific, and you just can't create such a column. After all hibernate finally sends DDL to the database. If you can't create a valid DDL with this column name, this means hibernate can't as well. I don't think quoting would solve the issue even if you are writing the DDL.

Even if you somehow succeed to escape the name - change it. It will work with this database, but won't work with another.

Solution 8 - Java

Some JPA implementations (e.g the one I use, DataNucleus) automatically quote the identifier for you, so you never get this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTJRView Question on Stackoverflow
Solution 1 - JavaPascal ThiventView Answer on Stackoverflow
Solution 2 - JavaRafiekView Answer on Stackoverflow
Solution 3 - JavaVlad MihalceaView Answer on Stackoverflow
Solution 4 - JavaRamanView Answer on Stackoverflow
Solution 5 - JavawmnitinView Answer on Stackoverflow
Solution 6 - JavaAhmed AshourView Answer on Stackoverflow
Solution 7 - JavaBozhoView Answer on Stackoverflow
Solution 8 - JavaNeil StocktonView Answer on Stackoverflow