0

How can I achieve "ON DELETE RESTRICT" behavior in a @OneToMany relationship?

AFAIK if I do not set CascadeType.REMOVE the children will not be deleted but the parent will still be removed. I know this cascade type is meant for entity objects but in the database the foreign keys all have "NO ACTION". How can I set the cascade type on database level?

I'm also astonished I did not find anything useful with google regarding this topic?

beginner_
  • 7,230
  • 18
  • 70
  • 127
  • Which database are you using? These options aren't SQL standard AFAIK. Anyway, this is not covered by JPA because JPA is not an API to generate a database schema. Implementations provide this possibility in proprietary ways, but it's mainly used in early prototyping. – JB Nizet Apr 17 '13 at 06:55
  • See http://en.wikipedia.org/wiki/Foreign_key#RESTRICT. This is standard SQL. I use PostgreSQL. basically I can just use NO ACTION then (my personal preference is to use RESTRICT but doesn't really matter). – beginner_ Apr 17 '13 at 07:18
  • 1
    What prevents you from setting the option you want in your DDL script? – JB Nizet Apr 17 '13 at 07:36
  • As JB Nizet says, creating a DDL script yourself is the recommended way by most JPA implementations. JPA2.1 will allow specification of such things, but that isn't final yet (nor are any implementations fully implementing all of it). – DataNucleus Apr 17 '13 at 07:54

1 Answers1

0

See comment. "NO ACTION" does what I want. If the delete violates a constraint, it will not be executed and an error is thrown.

beginner_
  • 7,230
  • 18
  • 70
  • 127