• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does Cascade anototation is equivalent to Cascading in db schema ?

 
Pramma Moorthy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi "Always Curious",
Welcome to javaranch. We are happy to help you, but at the same time we have some strict but friendly rules.
Please find our Naming Policy.
You can change your name here.
 
Pramma Moorthy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey James, thanks. that was helpful.
Hi Arun. thanks for reminding me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic