title: Remove Foreign Key constraint error in spring boot
body: <p><strong>Hello Guys</strong>, Hope you are doing well. I am working on my <strong>spring boot</strong> project where I add two entities <em><strong>Category</strong></em> and <em><strong>Item</strong></em> and the relationship between them is <em><strong>OneToMany</strong></em> i.e. One <strong>Category</strong> can have multiple <strong>items</strong> and many items can have only one <strong>category</strong>.
  According to my project requirement When the user deletes the <strong>category</strong> then its associate <strong>items</strong> will also be deleted. But when I try to delete the <strong>category</strong> it says;</p>
  <pre><code>    Cannot delete or update a parent row: a foreign key constraint fails 
  (`srms_db`.`issued_item`, CONSTRAINT `FK64cj06b00l58h8yi4msjuljll` FOREIGN KEY (`ii_fk`) REFERENCES `item` (`item_id`))
  </code></pre>
  <p>I am giving some code for more explanation;</p>
  <p><strong>Category Entity Code</strong></p>
  <pre><code>@Entity
  @Data
  @AllArgsConstructor
  @NoArgsConstructor
  @Table(name = &quot;Category&quot;)
  public class Category implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;cat_id&quot;, nullable = false)
      private int catId;
  
      @Column(name = &quot;cat_type&quot;, nullable = false)
      private String catType;
  
      @Column(name = &quot;cat_desc&quot;, nullable = true)
      private String catDesc;
  
      @OneToMany(mappedBy = &quot;category&quot;, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      private List&lt;Items&gt; items;
  </code></pre>
  <p><em>And the</em></p>
  <p><strong>Item Entity Code</strong></p>
  <pre><code>@Entity
  @AllArgsConstructor
  @NoArgsConstructor
  @Data
  @Table(name = &quot;Item&quot;)
  public class Items implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;item_id&quot;)
      private int itemId;
  
      @Column(name = &quot;item_name&quot;, nullable = false)
      private String itemName;
  
      @Column(name = &quot;item_quantity&quot;, nullable = false)
      private int itemQuantity;
  
      @Column(name = &quot;item_received_date&quot;, nullable = false)
      private Date itemReceivedDate;
  
      @OneToMany(mappedBy = &quot;item&quot;, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      private List&lt;DamageItem&gt; damageItems;
  
      @OneToMany(mappedBy = &quot;item&quot;, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      private List&lt;IssuedItem&gt; issuedItems;
  
      @ManyToOne
      @JoinColumn(name = &quot;ic_fk&quot;)
      private Category category;
  }
  </code></pre>
  <p><strong>Note</strong>
  <em><code>I also try on cascade=CascadeType.REMOVE, DETACH, REFRESH, etc but futile</code></em></p>
  <p><strong>Issued Item Entity</strong></p>
  <pre><code>@Entity
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  @Table(name = &quot;Issued_Item&quot;)
  public class IssuedItem implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;issued_id&quot;, nullable = false)
      private int IssuedItemId;
  
      @Column(name = &quot;issued_from&quot;, nullable = false)
      private String IssuedFrom;
  
      @Column(name = &quot;issued_to&quot;, nullable = false)
      private String IssuedTo;
  
      @Column(name = &quot;issued_quantity&quot;, nullable = false)
      private int IssuedQuantity;
  
      @Column(name = &quot;issued_date&quot;, nullable = false)
      private Date IssuedDate;
  
      @ManyToOne
      @JoinColumn(name = &quot;ii_fk&quot;)
      private Items item;
  }
  </code></pre>
  <p>I am also providing the <em><strong>damage Item Entity</strong></em> code</p>
  <pre><code>@Entity
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  @Table(name = &quot;Damage_Item&quot;)
  public class DamageItem implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;damage_id&quot;, nullable = false)
      private int DamageItemId;
  
      @Column(name = &quot;damage_quantity&quot;, nullable = false)
      private int DamageQuantity;
  
      @Column(name = &quot;damage_date&quot;, nullable = false)
      private Date DamageDate;
  
      @ManyToOne
      @JoinColumn(name = &quot;id_fk&quot;)
      private Items item;
  }
  </code></pre>
  <p><strong>Thanks In Advance.</strong></p>