開發的系統,今天客戶回報新增資料(插入到oracle)失敗。檢查日志,報錯為:
ORA-00001: unique constraint (****.PK_T_***) violated
就是新增資料的主鍵值與資料庫裡面的資料重複了。
程式使用的是hibernate架構,用hibernate中的save() 方法來直接往資料庫中插入資料。
主鍵生成政策為:
<id name="Id" type="long">
<column name="Id" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">SEQ_T_***</param>
</generator>
</id>
檢查之後,發現是前一天在資料庫中直接insert大批資料,導緻資料庫中的sequence SEQ_T_*** 沒有相應更新。今天通過sequence來自動生成主鍵的時候,就生成了重複的主鍵值。修改資料庫中的sequence起始值為目前的最大的主鍵id值+1即可。
如圖: