背景:用Eclipse+MySQL做项目的时候,从项目中保存数据
问题:
出现错误
org.springframework.transaction.TransactionSystemException:Could not commit JPA transaction;
nested exception isjavax.persistence.RollbackException: Exception [EclipseLink-4002]
(Eclipse Persistence Services -2.4.0.v20120608-r11652):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry '2' for key 'PRIMARY'
Error Code: 1062
关键信息:
1、MySQLIntegrityConstraintViolationException是指外键出错了(我的问题是所改主键作为了其他表的外键)
2、Duplicate entry '2'for key 'PRIMARY'是指主键重复了
解决办法:把数据库中那条主键为2的数据的主键改为其他值,再执行保存,就OK啦!