Exception on cascade of remove in a unidirectional @OneToMany
posted 6 years ago
I have defined a (MS SQL Server 2008) database for a phone book as following:
Note that the DBA (which is me ) have decided that a subscriber that not have any phone numbers may exist but a phone number must always have a subscriber, that is, the foreign key in PhoneNumber for a subscriber must not be null!
The code in an application that executes this phonebook have this method for remove of a subscriber:
The relation between a subscriber and a phone number is defined as a unidirectional one-to-many relation in subscriber like this
Note that remove of a subscriber should cascade to its phone numbers. When a subscriber is removed should all the phone numbers also be removed.
When the remove is committed in the removeSubscriber method is an exception thrown:
I am currently reading the good book Pro JPA 2: Mastering the Java Persistence API but I have not yet read anything about what the rule is for a situation like that above. To get the application work do I have to make the foreign key subscriber in the PhoneNumber table nullable but I don't know if it is because of a bug in the OpenJPA 2.1 implementation I use or if it is specified to work this way in the JPA 2 specification.
Does anyone know which of those alternatives is true?
In my opinion should I not have to customize the design of the database lika that. Instead should the entity manager in this case with a cascading remove first just remove the phone number and then without any problem remove the subscriber.
Truly agile Java application developer with Extreme Programming and high quality craftsmanship