我将通过ID删除对象,但出现类似以下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败(mydatabase
。assignmentaudit
,CONSTRAINT FKqgsllo6dm5kdx7ptc3qu5mefd
外键(auditor_group_id
)参考auditor_group
(auditor_group_id
))
我通过ID删除的方法是:
@DeleteMapping("/assignment-audit/{id}")
public ResponseEntity<String> deleteAssignmentAudit(@PathVariable("id") long id) {
System.out.println("Delete AssignmentAudit with ID = " + id + "...");
assignmentAuditRepository.deleteById(id);
return new ResponseEntity<>("Assignment Audit has been deleted!", HttpStatus.OK);
}
我的表的映射如下:
Selection.java
@Entity
@Table(name="selection")
public class Selection implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long selectionId;
@Column(name="selection_date")
private String selectionDate;
@Column(name="selected_by")
private String selectedBy;
@Column(name="pan_exim_number")
private Long panEximNumber;
@Column(name="name")
private String name;
@Column(name="address")
private String address;
@Column(name="phone_number")
private String phoneNumber;
@Column(name="selection_type")
private String selectionType;
@Column(name="consignment_no")
private String consignentNo;
@Column(name="consignment_date")
private String consignentDate;
@Column(name="selection_period_from_date")
private String selectionPeriodFromDate;
@Column(name="selection_period_to_date")
private String selectionPeriodToDate;
@Column(name="agent_no")
private Long agentNo;
@Column(name="custom_office")
private String customOffice;
@OneToMany(mappedBy="selection")
private List<AssignmentAudit> assignmentAudit;
//i omitted getters and setters as i have in my program
}
AssignmentAudit.java
@Entity
@Table(name = "assignmentaudit")
public class AssignmentAudit implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.ALL)
@JoinColumn(name = "auditorGroupId")
private AuditorGroup auditorGroup;
@Column(name = "assignmentDate")
private String assignmentDate;
@ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.ALL)
@JoinColumn(name = "selectionId")
@JsonIgnore
private Selection selection;
}
AuditorGroup.java
@Entity
@Table(name = "auditor_group")
public class AuditorGroup implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long auditorGroupId;
@Column(name="group_desc")
private String groupDesc;
@Column(name="from_date")
private String fromDate;
@Column(name="to_date")
private String toDate;
}
AssignmentAuditRepository.java
public interface AssignmentAuditRepository extends JpaRepository<AssignmentAudit, Long> {
}
我只需要删除assignmentAudit
的{{1}},以使id
和selection
保持不变,并且在删除auditGroup
时不受影响。
答案 0 :(得分:1)
您必须从cascade=CascadeType.ALL
映射中删除@ManyToOne
。
或者更改它,以便不包括CascadeType.DELETE
。