【发布时间】:2021-11-04 08:36:07
【问题描述】:
我正在使用此代码在 Spring Boot 中更新 mysql 5.7 表:
@Override
public Integer updateOrInsert(SparkGameUserScore score) {
SparkGameUserScoreExample example = new SparkGameUserScoreExample();
SparkGameUserScoreExample.Criteria criteria = example.createCriteria();
criteria.andGameIdEqualTo(score.getGameId());
criteria.andUserIdEqualTo(score.getUserId());
criteria.andTargetUserIdEqualTo(score.getTargetUserId());
criteria.andVoteRoundEqualTo(score.getVoteRound());
List<SparkGameUserScore> scores = sparkGameUserScoreMapper.selectByExample(example);
if (CollectionUtils.isEmpty(scores)) {
return sparkGameUserScoreMapper.insert(score);
} else {
return sparkGameUserScoreMapper.updateByExampleSelective(score, example);
}
}
有时这段代码会显示如下错误:
at com.sportswin.soa.spark.biz.GuestMatch.calcSparkScore(GuestMatch.java:97) ~[classes!/:na]
at com.sportswin.soa.spark.biz.GuestMatch.calcGuestMatchCalc(GuestMatch.java:73) ~[classes!/:na]
at com.sportswin.soa.spark.controller.impl.SparkGameUserController.calcScore(SparkGameUserController.java:106) ~[classes!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_252]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_252]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_252]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_252]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) [spring-web-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) [spring-web-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) [spring-webmvc-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893) ~[spring-webmvc-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798) ~[spring-webmvc-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) [spring-webmvc-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
... 63 common frames omitted
Caused by: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '84' for key 'PRIMARY'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.ClientPreparedStatement.execute$original$Jp9xGgoY(ClientPreparedStatement.java:370) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
at com.mysql.cj.jdbc.ClientPreparedStatement.execute$original$Jp9xGgoY$accessor$avRe8pW7(ClientPreparedStatement.java) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
为什么要告诉SQLIntegrityConstraintViolationException: Duplicate entry '84' for key 'PRIMARY' 错误?此代码有时可以正常工作,有时会抛出错误。我确定执行更新操作时没有设置实体id值。
【问题讨论】: