aspose file tools
The moose likes Object Relational Mapping and the fly likes Exception on cascade of remove in a unidirectional @OneToMany Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Exception on cascade of remove in a unidirectional @OneToMany" Watch "Exception on cascade of remove in a unidirectional @OneToMany" New topic
Author

Exception on cascade of remove in a unidirectional @OneToMany

Jonny Andersson
Ranch Hand

Joined: Sep 07, 2004
Posts: 85
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.


Effective Java Programming Language Guide: Think in terms of exported APIs because it tends to improve the quality of the software you write
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception on cascade of remove in a unidirectional @OneToMany
 
Similar Threads
EJB 3 in Action - JPA Doubt
mapping One-to-many relationships using NetBeans and Not Hibernate
Cannot add or update a child row: a foreign key constraint fails JPA/Hibernate
Restore problem
Glassfish + jdbcrealm