com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails










0














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:



enter image description here



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.










share|improve this question


























    0














    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:



    enter image description here



    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.










    share|improve this question
























      0












      0








      0







      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:



      enter image description here



      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.










      share|improve this question













      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:



      enter image description here



      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.







      java spring hibernate spring-boot jpa






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 7:08









      ashwin karki

      1909




      1909






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You have to remove the cascade=CascadeType.ALL from the @ManyToOne mappings.



          Alternatively change it, so that the CascadeType.DELETE is not included.






          share|improve this answer




















          • how can i only neglect cascadetype.delete from all types? i need exact code to change it
            – ashwin karki
            Nov 12 at 7:21










          • thnk u i changed and worked :) really appreciated ur help
            – ashwin karki
            Nov 12 at 7:24










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257329%2fcom-mysql-jdbc-exceptions-jdbc4-mysqlintegrityconstraintviolationexception-cann%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You have to remove the cascade=CascadeType.ALL from the @ManyToOne mappings.



          Alternatively change it, so that the CascadeType.DELETE is not included.






          share|improve this answer




















          • how can i only neglect cascadetype.delete from all types? i need exact code to change it
            – ashwin karki
            Nov 12 at 7:21










          • thnk u i changed and worked :) really appreciated ur help
            – ashwin karki
            Nov 12 at 7:24















          1














          You have to remove the cascade=CascadeType.ALL from the @ManyToOne mappings.



          Alternatively change it, so that the CascadeType.DELETE is not included.






          share|improve this answer




















          • how can i only neglect cascadetype.delete from all types? i need exact code to change it
            – ashwin karki
            Nov 12 at 7:21










          • thnk u i changed and worked :) really appreciated ur help
            – ashwin karki
            Nov 12 at 7:24













          1












          1








          1






          You have to remove the cascade=CascadeType.ALL from the @ManyToOne mappings.



          Alternatively change it, so that the CascadeType.DELETE is not included.






          share|improve this answer












          You have to remove the cascade=CascadeType.ALL from the @ManyToOne mappings.



          Alternatively change it, so that the CascadeType.DELETE is not included.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 7:14









          Maciej Kowalski

          11.4k72037




          11.4k72037











          • how can i only neglect cascadetype.delete from all types? i need exact code to change it
            – ashwin karki
            Nov 12 at 7:21










          • thnk u i changed and worked :) really appreciated ur help
            – ashwin karki
            Nov 12 at 7:24
















          • how can i only neglect cascadetype.delete from all types? i need exact code to change it
            – ashwin karki
            Nov 12 at 7:21










          • thnk u i changed and worked :) really appreciated ur help
            – ashwin karki
            Nov 12 at 7:24















          how can i only neglect cascadetype.delete from all types? i need exact code to change it
          – ashwin karki
          Nov 12 at 7:21




          how can i only neglect cascadetype.delete from all types? i need exact code to change it
          – ashwin karki
          Nov 12 at 7:21












          thnk u i changed and worked :) really appreciated ur help
          – ashwin karki
          Nov 12 at 7:24




          thnk u i changed and worked :) really appreciated ur help
          – ashwin karki
          Nov 12 at 7:24

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257329%2fcom-mysql-jdbc-exceptions-jdbc4-mysqlintegrityconstraintviolationexception-cann%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3