I am going to delete the object by id but i am getting error like:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (mydatabase.assignmentaudit, CONSTRAINT FKqgsllo6dm5kdx7ptc3qu5mefd FOREIGN KEY (auditor_group_id) REFERENCES auditor_group (auditor_group_id))
The method i made to delete by id is :
@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); } The mapping of my tables looks like:
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> { } I only need to delete assignmentAudit by its id such that selection and auditGroup remains unchanged and unaffected when assignmentAudit is deleted.
