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

前端 未结 7 1569
陌清茗
陌清茗 2020-11-22 12:35
@Column(name=\"open\")

Using sqlserver dialect with hibernate.

[SchemaUpdate] Unsuccessful: create table auth_session (id numeric(1         


        
相关标签:
7条回答
  • 2020-11-22 13:10

    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
    0 讨论(0)
提交回复
热议问题