在 Java Web 專案(Struts + Hibernate + Oracle)中執行批次更新時,拋出以下異常:
Struts Problem Report
Struts has detected an unhandled exception:
Messages: ORA-00001: unique constraint (REQUIREMENTS.PK_USER_LOGINLOG_ID) violated
Could not execute JDBC batch update
Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
File: oracle/jdbc/driver/DatabaseError.java
Line number: 342
ORA-00001 表示試圖插入的資料違反了唯一約束條件。具體到這裡,主鍵 PK_USER_LOGINLOG_ID 發生了重複。常見原因包括:
<generator> 配置正確,Oracle 建議使用 sequence。setId(),請移除,交由框架管理。SELECT seq.nextval FROM dual 推進序列。session.save() 循環插入,請定期 flush() 和 clear(),避免快取導致的重複提交。修正後重新部署測試即可恢復正常。