保存集时违反Spring JPA唯一约束

时间:2018-11-05 09:32:00

标签: mysql spring hibernate jpa

即使添加唯一委托对象,也遇到了唯一约束冲突。下面是我的映射。

工作流程实体

@Table(name = "wkf_inst")

@Id
@Column(name = "WKF_INST_ID", updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer wkfInstId;

@OneToMany(mappedBy = "wkfInstance", fetch = FetchType.EAGER, cascade = 
CascadeType.ALL)
private Set<Delegation> Delegations;

委托实体

@Table(name = "wkf_assignee")
@Id
@Column(name = "WKF_INST_ASSIGNED_ID", updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer wkfDelegationId;

@Column(name = "ASSIGNED_TO")
private String delegatedTo;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "WKF_INST_ID")
private WkfInst wkfInstance;

我们在wkf_assignee表(WKF_INST_ID,ASSIGNED_TO)上具有唯一约束WKF_DELEGATION_UK。

我已将4个协商对象作为一组添加到Workflow对象,并保存了wkfInstRepository.saveAndFlush(wkfInst)。

保存会引发唯一违反约束的异常。

.d.i.w.controller.WorkflowController    : Update Workflow error for the workflow with instance ID : [248134]  [Co
mmon Workflow Engine] could not execute statement; SQL [n/a]; constraint [WKF_DELEGATION_UK]; nested exception is org.hibernate.exception.ConstraintViolationException: could 
not execute statement org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:278) 

从下面的列表中提取委托对象。

`workflowInstance.getAssignedToList()
            .stream()
            .map(o -> {
                WkfDelegation delegation = new WkfDelegation();
                delegation.setDelegatedTo(o.getAssignedTo());
                delegation.setPriority(1);
                delegation.setDelegatedRoleId(o.getAssignedToRole());
                delegation.setIsActive("N");
                delegation.setWkfInstance(wkfInst);
                delegation.setDelegatedEmailId(o.getAssignedToEmailId());
                );
                return delegation;
            })
            .collect(Collectors.toSet());`

此委派设置为以下工作流程:

wkfInst.setWkfDelegations(getWorkFlowDelegations(workflowInstanceInfo, wkfInst));

0 个答案:

没有答案