Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 560
TJR Avatar asked Feb 08 '10 20:02

TJR


People also ask

Which annotation in JPA is used to customize the mapping of a field?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column. You can use the name attribute to specify the name of the database column which the entity attribute map.

How do I map a OneToOne JPA?

The best way to map a @OneToOne relationship is to use @MapsId . This way, you don't even need a bidirectional association since you can always fetch the PostDetails entity by using the Post entity identifier. This way, the id property serves as both Primary Key and Foreign Key.

Which JPA annotation is used for a property of a class to map it with a primary key?

The embedded id approach uses an embeddable to map the primary key attributes. An embeddable is a pure Java class that is annotated with @Embeddable. It defines attribute mappings in a reusable way.

What is @column in JPA?

In this article, we will discuss how to change the column name in the Spring project using JPA. @Column annotation is used for Adding the column the name in the table of a particular MySQL database. Syntax: @Column(name=”DESC”, nullable=false, length=512)


2 Answers

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.

<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

  • 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
like image 153
Pascal Thivent Avatar answered Sep 20 '22 18:09

Pascal Thivent


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.

like image 22
Rafiek Avatar answered Sep 22 '22 18:09

Rafiek