【问题标题】:SQLIntegrityConstraintViolationException: Duplicate entry '84' for key 'PRIMARY' when update mysql table using mybatisSQLIntegrityConstraintViolationException:使用 mybatis 更新 mysql 表时,键 'PRIMARY' 的重复条目 '84'
【发布时间】: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值。

【问题讨论】:

    标签: java mybatis


    【解决方案1】:

    这个异常看起来像是来自插入数据。 这可能是一个自动增量问题。 检查这个答案java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '0' for key 'PRIMARY'

    【讨论】:

    • 有时工作正常,有时显示此错误,我没有更改我的代码,但现在工作正常,让我感到困惑。 @Ravi Korrapati
    【解决方案2】:

    显示 mapper.xml 插入语句,像这样设置自动增量 id?

    <insert id="insert" parameterType="Book">
              INSERT INTO tab_products(prod_id,name,description)
          VALUES(SEQ_PRD_ID.nextVal,#{name},#{desc});
    </insert>
    

    【讨论】:

      猜你喜欢
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 2012-10-13
      • 2012-08-24
      • 2016-05-27
      • 2017-02-17
      相关资源
      最近更新 更多