1 Reply Latest reply on May 1, 2009 3:34 PM by lstoronto2009

    Trying to cascade a delete operation

    lstoronto2009

      I have a Company class:

      public class Company implements Serializable {
      
      ....
      
      @OneToMany (mappedBy="company", cascade=CascadeType.ALL)
       @JoinColumn(name="COMPANY_ID")
       private List<Address> addresses = new ArrayList<Address>();
      ...
      }
      


      The intention is a company can have one to many addresses.

      Here is the Address:

      public class Address implements Serializable {
      ....
      
      @ManyToOne
      private Company company;
      
      ...
      }
      


      The behavior I want is if a Company is deleted, all the associated Addresses should be removed as well.

      However when I delete a Company I get a constraint violation:
      com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`db`.`address`, CONSTRAINT `FK1ED033D44C674D5C` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`))
      


      I thought the cascade=CascadeType.ALL in the Company class would give me this.

      What am I doing wrong?