在测试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条记录时的内容一样,主键是自动生成的,所以出问题了。
博主在测试Hibernate时遇到重复键或完整性约束违规异常。经排查,发现是定义数据表字段时添加了not null unique,而一次添加多条相同内容记录,且主键自动生成,从而导致问题出现。
1678

被折叠的 条评论
为什么被折叠?



