Spring Jpa 實體類欄位與資料庫關鍵字衝突解決
阿新 • • 發佈:2018-12-25
一般情況下實體類欄位不建議取會與資料庫關鍵字相同的名字,但總會有些特殊情況
比如下面這個情況,在使用MySQL的時候會出現錯誤(但是使用h2的MySQL模式不會有問題)
@Entity
public class Category {
@GeneratedValue
@Id
private int id;
@Column(unique = true, nullable = false, length = 64)
private String name;
@Column
private String desc;
@Column (nullable = false)
private Date created;
@Column(nullable = false)
privte Date modified;
}
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [create table category (id integer not null auto_increment, created datetime not null , desc varchar(255), modified datetime not null, name varchar(64) not null, primary key (id))]
這裡是因為 desc 是 MySQL 裡面的關鍵字,導致轉換成sql語句後執行出現異常
解決方法
- 把對應的@Column 改為 @Column(name = “[desc]”) 或者自定義一個其他名字