在測試hibernate時出現了如下異常:
[Duplicate key or integrity constraint violation message from server: "Duplicate entry 'rick0@foobar.com' for key 2"]; SQL was [] for task [Hibernate operation]
org.springframework.dao.DataIntegrityViolationException: Hibernate operation: Duplicate key or integrity constraint violation message from server: "Duplicate entry 'rick0@foobar.com' for key 2"; nested exception is java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Duplicate entry 'rick0@foobar.com' for key 2"
java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Duplicate entry 'rick0@foobar.com' for key 2"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
這個問題找了很久,暈了很久,終于找到了問題的所在。因我在定義數據表字段時加了not null unique,而我一次添加N條記錄時的內容一樣,主鍵是自動生成的,所以出問題了。