2016-11-25 5 views
0

同じテーブルの異なる行のプロパティを交換する必要があります。
ユニークという制約をDBに持つ1つの列「reference_id」があります。BatchUpdateException重複するキー値が一意の制約に違反しています

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 
+0

バッチ更新によって一意制約例外が発生しています。たとえ私が故意にreferenceIdに "##"を付加していても、それが一意であるが、それも動作していないようにしています。バッチ更新が機能しないように、「各マージクエリに対して個別のトランザクション」のようなものがありますか? –

答えて

0

マージの間にエンティティマネージャをフラッシュしてみてください。

+0

ya、私はすでにそれをして、あなたは同じことを投稿しました。 –

関連する問題