title: Getting Hibernate to cascade on save, but not delete?
body: <p>So I'm having a problem with my hibernate implementation. When I try to delete a parent class, I receive a foreign key constraint exception on a class deep within the cascade hierarchy. Before I go into specifics, I'll first describe the relationships of the classes, as it has a bearing on how they need to be saved and deleted.</p>
  
  <p>At the top level, I have a Customer class, which contains a list of DefaultMask objects. This is the master list, in that these default masks are used by other classes in my object hierarchy, but always from this list. Masks are only created into this list and deleted from this list.</p>
  
  <p>Further down the hierarchy, I have a Column class, which can (optionally) have a DefaultMask set on it. To describe the relationship more succinctly;</p>
  
  <p>A Customer OWNS zero to many DefaultMasks.
  A Customer OWNS zero to many Columns.
  A Column may have one DefaultMask.</p>
  
  <p>In my application, when I attempt to delete a Customer, the exception comes from the foreign-key constraint on the Column class to the DefaultMask class, and I believe the problem is incorrect settings with CascadeType. I have researched the problem and found information on an attribute called mappedBy and on using Hibernate's own CascadeType.SAVE_UPDATE (in order to prevent Hibernate trying to delete a DefaultMask held by a Column), but I will admit I am a bit lost here and could use some direct guidance. Relevant code for the classes and the actual exception message are below.</p>
  
  <p>Customer:</p>
  
  <pre><code>@Entity
  public class Customer {
  
  @Id
  private String id;
  @OneToMany(cascade = CascadeType.ALL)
  private List&lt;DefaultMask&gt; masks;
      //(Columns are held further down in hierarchy)
  </code></pre>
  
  <p>Column:</p>
  
  <pre><code>@Entity
  @Table(name = "WarehouseColumn")
  public class Column implements Comparable&lt;Column&gt; {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int hibernateID;
  @OneToOne
  private DefaultMask mask;
  </code></pre>
  
  <p>DefaultMask:</p>
  
  <pre><code>@Entity
  public class DefaultMask implements Mask {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int hibernateID;
  private String type;
  private String mask;
  </code></pre>
  
  <p>Exception message:</p>
  
  <p>org.hibernate.exception.ConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (<code>hibernate</code>.<code>WarehouseColumn</code>, CONSTRAINT <code>FK8BB153D994AD57D3</code> FOREIGN KEY (<code>mask_hibernateID</code>) REFERENCES <code>DefaultMask</code> (<code>hibernateID</code>))
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (<code>hibernate</code>.<code>WarehouseColumn</code>, CONSTRAINT <code>FK8BB153D994AD57D3</code> FOREIGN KEY (<code>mask_hibernateID</code>) REFERENCES <code>DefaultMask</code> (<code>hibernateID</code>))</p>