• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Association problem in delete

 
Akhil Jain
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac 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
 
Bougnon Kipre
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?
 
Mike Keith
author
Ranch Hand
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic