Does Cascade anototation is equivalent to Cascading in db schema ?
Joined: Jul 16, 2009
When i annotate an element as say '@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)',
is that equivalent to cascade in db schema itself?
or to be precise, after creating such entity in db, if i execute a 'delete' query in db on primary entity, will it perform cascade delete?
Joined: Oct 01, 2007
No, not really.
cascade will not cause the database constraint to be created with cascade delete (but may be JPA provider dependent).
It only affects when the parent object is removed through JPA.
You can create your constraint with cascade delete if you desire, using your own DDL scripts. Some JPA providers support generating the cascade delete on the constraint if using their DDL generation support.
The latest builds of EclipseLink provide a @CascadeOnDelete annotation for this.
Note that cascade delete only applies to a constraint, and only works in the inverse direction of the constraint. Where as JPA cascade works in the direction of the relationship, on any relationship type. So a cascade delete in your database will only work if the constraint for your OneToOne is from the dependent object back to the parent object. If your OneToOne uses a JoinColumn, then a cascade delete in the database will not work.