1. 程式人生 > >Spring Data Jpa + Mysql實體類自動建立表時出現錯誤

Spring Data Jpa + Mysql實體類自動建立表時出現錯誤

實體類Param,設定表名為vbap3_sql_param,在執行之後,出現錯誤,錯誤的建表語句如下。(資料庫是用的Mysql)

create table vbap3_sql_param (id bigint not null auto_increment, display_name varchar(255), name varchar(255), anonymous bit not null, data_format varchar(255), data_type integer, default_value varchar(255), precision integer not null, removable_when_null bit not null, required bit not null, query_def_id bigint, primary key (id)

提示SQL語法錯誤:

check the manual that corresponds to your MySQL server version for the right syntax to use near 'precision integer not null, removable_when_null bit not null, required bit not n' at line 1

removable、 required的定義如下:

    private int precision;
    private boolean removableWhenNull;
    private boolean required;

解決方案:最後發現的問題是欄位precision是Mysql中的關鍵字,所以建表失敗,解決辦法就是把precision更改一個名字就可以了。

下面我說一下解決問題的過程:

1. 首先出現錯誤,發現是SQL語句的錯誤,我看就看到個bit,大驚,以為這個東西是個茬,以前沒接觸過這個型別,以為mysql不支援bit這個型別,但是我另外一個實體類中也有一個boolean型別的欄位,也成功建立了,是bit型別,所以這個問題排除了。

2. 既然是SQL語句的錯誤,那痛hibernate執行和在MYSQL中執行肯定是一樣的,複製生產的sql語句到Navicat工具中執行,還是出現同樣的錯誤。

3. 百度:發現有人說是關鍵字衝突,我一看有一個required,應該就是這個衝突,結果改為了sq_required,還是出現錯誤。(其實require是關鍵字,required不是)

4. 複製sql語句到Navicat中執行的時候發現SQL的關鍵字和欄位的顏色不一樣,發現了precision是一個關鍵字,這才解決了這個問題。

工具中的顏色顯示錶明瞭一切: precision integer not null, removable_when_nullbit not null, requiredbit not null

precision是一個關鍵字!!!!!