將 Tomcat + Struts + Hibernate + MySQL 專案移植至 SQL Server 2000,啟動時拋出:
Hibernate: select user0_.userid as x0_0_ from user user0_
SQL Exception: com.jnetdirect.jsql.JSQLException: 在關鍵字 'user' 附近有語法錯誤。
user 非保留字,可直接做資料表/欄位名稱user 為系統保留字 (系統資料表 sysusers 相關),直接使用會語法錯誤from user 解析失敗user 改為 employees、app_user、sys_user 等非保留字名稱<class table="employees"> 及 HQL 查詢 from Employee<class name="User" table="[user]"> <!-- SQL Server 方括號脫逸 -->
<!-- 或 -->
<class name="User" table="\"user\""> <!-- ANSI 雙引號脫逸 (需資料庫支援) -->
缺點: 降低可移植性,不同資料庫脫逸語法不同 (MySQL 用反引號 \`user\`,Oracle 用雙引號 "user"),維護成本高。
t_user、tbl_user) 或使用複數形式 (如 users)這個問題耗費半天排查,記錄下來警惕後人。