• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Association problem in delete

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am faving a problem with deletng some entries from a table.
Example:
Suppose, there is a table named Parent and Child. Child contains the reference of Parent. But parent doesn't know anything about that child of his.

When any data is deleted from Parent Table, the child data which contains the foreing key as that parent_id should also be deleted.

How can i do that?

Pleasenote: that parent does have many-to-one relationship with child hypothetically, but that is not defined explicitly in mappings.
Only child contains the reference of parent.

now, can someone please tell me, how i can set cascade delete operation in the scenario mentioned above, so that when someone deletes a parent tablle entry with id 1, then all the entries in child table which has foreing key (parent id) as 1 will automatically gets deleted.


Thanks
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, without that mapping, there is no way to tell your ORM that that is what you want, so you are basically left to implementing that code manually in your "delete" dao method for the parent.

You will have to take the parent's ID, do a query to get the children, then loop through the children passing it to delete to delete them first, then allow the parent to be deleted.

Mark
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Mark. No parent-child mapping, no cascading delete!
You have to write logic to do it.

Just out of curiosity, is there a reason why you have no collection mapping of
Parent-Child relationship?
 
author
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleasenote: that parent does have many-to-one relationship with child hypothetically, but that is not defined explicitly in mappings.
Only child contains the reference of parent.

now, can someone please tell me, how i can set cascade delete operation in the scenario mentioned above, so that when someone deletes a parent tablle entry with id 1, then all the entries in child table which has foreing key (parent id) as 1 will automatically gets deleted.
Akhil,

It sounds like the parent has a one-to-many relationship to the children, not a many-to-one (otherwise doing a cascade delete would not be good).

If you can, I would actually just create a property in the parent to point to the children and make it private, lazy loaded, and cascade delete. That way it is, for all intents and purposes, invisible and cost-free to everyone except the persistence engine, but when the parent delete happens the children will get automatically deleted as well.
 
Bougnon Kipre
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming this a one-to-many association, try using


Please made sure the syntax is correct
[ December 21, 2007: Message edited by: Bougnon Kipre ]
 
No, tomorrow we rule the world! With this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!