112
@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?

1

8 Answers 8

156

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 Hibernate 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:

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

References

Related questions

5
  • And thanks from me. It solved a problem I had. btw - Ref is now at: docs.jboss.org/hibernate/stable/core/manual/en-US/html/…
    – Steve
    Nov 12, 2013 at 16:54
  • 7
    I don't understand why I have to do this, why Hibernate don't do this automatically instead of me??? May 4, 2016 at 22:54
  • @DanielHári maybe you find my answer more "automatic"?
    – Rafiek
    Apr 25, 2018 at 11:08
  • 1
    Using @Column(name="[open]") is much prettier :) Jun 10, 2018 at 10:36
  • If you use @Column(name="\"open\"") on Oracle, note that quoted identifiers are case sensitive. Which means most likely you need to use @Column(name="\"OPEN\"") instead, otherwise you'll get an invalid identifier error. Dec 1, 2019 at 11:39
70

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.

6
  • 12
    How is this not the default setting?
    – Josh M.
    Jun 28, 2018 at 14:21
  • SQL might become unreadable and using keywords as names is a bad practice that should not be encouraged. I think...?
    – Rafiek
    Jun 29, 2018 at 9:53
  • 3
    Okay. I'll prefer an escaped reserved word as a name all day long over a name that doesn't fit.
    – Josh M.
    Jun 29, 2018 at 15:13
  • 1
    Yes, you could say that the abstraction provided by Hibernate is only of concern and not how it is technically implemented. But if you also use tooling like Flyway or Liquibase, then it adds to complexity when you need to consider that there might be reserved words. This has been my experience when migrating schema's.
    – Rafiek
    Jun 30, 2018 at 6:56
  • 2
    For those wondering where this needs to be set, it's probably in your persistence.xml for JBoss projects.
    – Addison
    Feb 4, 2019 at 6:10
28

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
0
17

If you use as shown below it should work

@Column(name="[order]")
private int order;
2
  • You're just doing this on the private field not on the getter? Mar 2, 2018 at 16:06
  • 6
    this is sqlserver specific.
    – Alfredo M
    Jul 3, 2018 at 21:49
13
@Column(name="\"open\"")

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

12

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
2
  • 2
    I found this better than globally_quoted_identifiers because this only quotes reserved keywords instead of every identifier. Jan 4, 2022 at 1:30
  • 1
    It's a shame this isn't set by default, I can see that not setting this makes it harder to switch to another JPA implementation that doesn't support automatic quoting. Jun 28, 2022 at 17:07
3

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.

2
  • That may work. See stackoverflow.com/questions/285775/…. Let's wait for the OP confirmation.
    – ewernli
    Feb 8, 2010 at 20:18
  • 1
    This is not database-specific! You escape it with a ` and hibernate translate it to correct quotation style for the SQL Dialect Jun 26, 2017 at 12:24
3

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

1
  • Yes, surprised that Hibernate seemingly still does not offer such a basic feature given the number of times people get hit by it (and someone even downvoted you for daring to mention that this was possible elsewhere)
    – user3973283
    Jul 9, 2018 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.