BatchUpdateException重复键值违反唯一约束

时间:2016-11-25 07:45:44

标签: java spring hibernate jpa

我必须交换同一个表的不同行的属性 有一列“reference_id”在DB中有唯一约束。

CODE:

A类 -

@Override
@Transactional
@ResponseBody
@RequestMapping(value = "/cust/{fromCustId}/{toCustId}/swapCusts", method = { RequestMethod.PUT })
public void swapContents(@PathVariable("fromCustId") final long fromCustId, @PathVariable("toCustId") final long toCustId) throws InvalidCustException
{           
    custService.swapContents(fromCustId, toCustId);     
}

B类 -

 @Override
          public void swapContents(long fromCustId, long toCustId) throws InvalidCustException {
           try {
            CustEntity fromCustEntity = custManager.findByPrimaryKey(fromCustId);
            CustEntity toCustEntity = custManager.findByPrimaryKey(toCustId);

            if (null == fromCustEntity || null == toCustEntity) {
             throw new InvalidCustException("Either fromCustId=[" + fromCustId + "] or toCustId=[" + toCustId + "] is invalid");
            }

            String fromCust_RefId = fromCustEntity.getReferenceId();
            String fromCust_Password = fromCustEntity.getPassword();

            String toCust_RefId = toCustEntity.getReferenceId();
            toCustEntity.setReferenceId("##" + toCust_RefId);
            custManager.merge(toCustEntity);

            fromCustEntity.setReferenceId(toCust_RefId);
            fromCustEntity.setPassword(toCustEntity.getPassword());


            toCustEntity.setReferenceId(fromCust_RefId);
            toCustEntity.setPassword(fromCust_Password);

            custManager.merge(fromCustEntity);
            custManager.merge(toCustEntity);

           } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
           }
          }

异常追踪:

   2016-11-24 15:14:26,772 WARN  [http-nio-8093-exec-8] or.hi.en.jd.sp.SqlExceptionHelper (SqlExceptionHelper.java:144) - SQL Error: 0, SQLState: null 2016-11-24 15:14:26,774 ERROR [http-nio-8093-exec-8] or.hi.en.jd.sp.SqlExceptionHelper (SqlExceptionHelper.java:146) - PerfPreparedStatement.executeBatch - update cust set  password='30ca904c-f720-4994-9e1a-dc5153c1cd85', reference_id='3355efa8-53df-4220-be32-752ce9a54645', cust_uuid=? where cust_id=82737521 2016-11-24 15:14:26,780 ERROR [http-nio-8093-exec-8] or.hi.en.jd.ba.in.BatchingBatch (BatchingBatch.java:141) - HHH000315: Exception executing batch [could not execute batch] 2016-11-24 15:14:26,822 WARN  [http-nio-8093-exec-8] co.gr.en.pu.ut.CommonExceptionConverterLogic (CommonExceptionConverterLogic.java:139) - org.hibernate.exception.GenericJDBCException: could not execute batch for User ID: 805, Request ID: b9167f59-87ee-498e-8aba-f505d2dea195, clientIP: 10.201.141.120 method: PUT uri: /custApi/services/cust/82737521/82737513/swapCusts, Request Parameters: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "tuc_cust_1"   Detail: Key (reference_id)=(41e24e58-a108-4f12-9b6b-4250a7a379e5) already exists.      
   at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)      
   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886) [wrapped] java.sql.BatchUpdateException: Batch entry 0 update cust set password='530a32c1-eba5-4de8-8e85-97d3af62c331', reference_id='41e24e58-a108-4f12-9b6b-4250a7a379e5', cust_uuid=? where cust_id=82737513 was aborted.  Call getNextException to see the cause.      
   at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2746)      
   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1887)      
   at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:405)      
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2893)      
   at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723) [wrapped] java.sql.SQLException: PerfPreparedStatement.executeBatch - update cust set password='30ca904c-f720-4994-9e1a-dc5153c1cd83', reference_id='3355efa8-53df-4220-be32-752ce9a54645', cust_uuid=? where cust_id=82737521

1 个答案:

答案 0 :(得分:0)

尝试在合并之间刷新实体管理器。