(Transfer) Using database keywords (reserved words) in hibernate

 In hibernate , when an entity object uses database reserved words as field names (although not recommended, but must be used in rare cases), you may encounter the following error when performing the save operation.

ERROR JDBCExceptionReporter:78 - You have an error in your SQL syntax; 

check the manual that corresponds to your MySQL server version for the 

right syntax to use near 'Datadabase reserved keyword....

E.g:

In MySql, "desc" is a reserved word, we have two ways to achieve it.

1. Use []

<property name="desc" type="string" >
    <column name="[DESC]" length="255" not-null="true" />
</property>

annotation

@Column(name = "[DESC]", nullable = false)
public String getDesc() {
return this.desc;
}

2. Use single quotes to surround double quotes

<property name="desc" type="string" >
    <column name='"DESC"' length="255" not-null="true" />
</property>

annotation

@Column(name = "\"DESC\"", nullable = false)
public String getDesc() {
return this.desc;
}

This way also works for table names.

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326780525&siteId=291194637