aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate - Deleting a parent and it's children Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate - Deleting a parent and it Watch "Hibernate - Deleting a parent and it New topic
Author

Hibernate - Deleting a parent and it's children

Casey Kcins
Greenhorn

Joined: Mar 12, 2007
Posts: 16
Hi there!
I need some help understanding something/getting it to work.
In a previous post (http://www.coderanch.com/t/217070/ORM/java/Hibernate-one-many), I was having a hard time understanding how to get one-to-many collections to work. Thanks to your help, I managed to figure it out. Now, I'm trying to figure out how to delete the collections (and then the parent).
Now. What I fiugure, is when you have a parent, with children in a one-to-many relationship, and having cascade set to "all-delete-cascade", when you delete the parent, it should cascade over and delete the children also.

But that doesn't seem to be the case. I just get a foeign key error.

So. I figure I must delete the children first! But how...

I have 2.5 classes: a Doctor class, and an Appointment class.
Doctors have appointments.

Here's the Doctor class:


...and here's the Appointment Class:


...and because I'm neurotic (or a good developer, I have a subclass called DoctorsApointment. It just creates an instance of Doctor, and it's getter and setters.

Finally, here's my code where I attempt to delete what I created:


Now... What I'm trying to do, is first delete all the appointments, then the doctor. When I run it, I still get a "MySQLIntegerityConstraintVoiletingException", which is bascially telling me I'm voilating the relationship by deleting the doctor.

Can y'all help me make sense of this?

Thanks!

Casey Kcins
[ March 21, 2007: Message edited by: Casey Kcins ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I believe the cascade option you are looking for is not

"all-delete-cascade"

but

"all, delete-orphan"

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Casey Kcins
Greenhorn

Joined: Mar 12, 2007
Posts: 16
Yeah, I had in there like that. I typed it wrong here.

Still doesn't work though...
[ March 23, 2007: Message edited by: Casey Kcins ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hibernate - Deleting a parent and it's children