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

    Trying to cascade a delete operation

    Luke Shannon Newbie

      I have a Company class:

      public class Company implements Serializable {
      @OneToMany (mappedBy="company", cascade=CascadeType.ALL)
       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 {
      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?