使用JPA更新时违反唯一标识符

时间:2018-08-03 11:35:08

标签: hibernate jpa spring-data-jpa

我正在尝试更新表,但不断收到错误消息,提示ORA-00001: unique constraint

在列表.itemList(product.getproductItemList())上失败

我添加了它来设置产品:

updatedProducts.getProductItemList().forEach(product -> product.setProduct(updatedProduct));

1 个答案:

答案 0 :(得分:1)

此行

.campaignItemList(newCampaign.getCampaignItemList())

添加

getCampaingsItemList().foreach(item=>item.setCampaing(updatedCampaing));

CampaingItem是存储关系(FK的所有者)的地方

Campaign updatedCampaign = ...builder stuff...
                                   .build(); <-- HERE YOU SEE I HAVE ENDED BUILDED MANIPULATION


//This is separate line of code executed somewhere before saveAndFlush and after `build();`    
updatedCampaing.getCampaingsItemList().forEach(item=>item.setCampaing(updatedCampaing));

恕我直言,这里发生了什么,JPA尝试创建新的露营项目,但是使用“旧”关系(露营ID +项目ID)导致违反约束。仍然是一种猜测,因为您提到的是代码中不存在的一些删除。