Hibernate version:
Core 3.2.5, Annotations 3.3.0
I have a simple pair of parent and child classes where the child has a reference to the parent in a @ManyToOne relationship as show below. I implemented a Cascade Delete using the @OnDelete(OnDeleteCascade.DELETE) annotation and it works fine.
I would like to be able to deploy this change to older installations where this annotation did not exist and thus the database DDL was missing the ON DELETE CASCADE constraint when it was created. I would like to avoid deploying upgrade scripts or procedures to the database.
So I tried getting the Hibernate-level cascade delete to work with @Cascade(CascadeType.DELETE) assuming this would be the answer (correct?). I'm not sure if I am doing something wrong or if the @Cascade is not properly supported for @ManyToOne relationships but I could not get it to work.
Here's an extract of my current code:
Child
@Entity
@Table(name="mychild")
public class MyChild implements Serializable {
private int id;
private MyParent parent;
@ManyToOne
@JoinColumn(name = "parent_id")
@OnDelete(action = OnDeleteAction.CASCADE)
public MyParent getParent() {
return parent;
}
}
Parent
@Entity
@Table(name="myparent")
public class MyParent implements Serializable {
private int id;
private String parentName;
}
Among many other things, I tried replacing @OnDelete(action = OnDeleteAction.CASCADE) with @Cascade(CascadeType.DELETE) and calling session.delete(myparent) but it kept failing with a "foreign constraint exception" in MySQL.
What am I missing?
Thanks,
Ralph
|