views:

328

answers:

4
@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  A: 

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.

Bozho
That *may* work. See http://stackoverflow.com/questions/285775/how-to-deal-with-sql-column-names-that-look-like-sql-keywords. Let's wait for the OP confirmation.
ewernli
Ended up changing it.
TJ
+1 for portability
ewernli
A: 

In DataNucleus you can have your table columns whatever you want. If they just happen to be reserved words then we quote them for you.

DataNucleus
+1  A: 

If you use as shown below it should work

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

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

Related questions

Pascal Thivent