• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate 2 deleting by cascade

 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have Persistent class A and persistent class B.
Class B has a field that's the hibernate id (key) of A. What cascade do I need to set and where so that when I delete object of type A all the objects of type B that contain the id of of this A object are deleted.?
it's
cascade = "all-delete-orphan" right?
Thanks,
John
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to enable cascade you should have relation between class A and class B. so supposing that A has many B objects then in class A there should be a property like this one:

Set<B> bees = new HashSet<B>();

and then by specifying the correct mappings using annotations or xml, and adding the correct cascade. when you delete class A, all objects in the set will be also deleted from the DB.

if you don't know how to map the entities then google for "many to one mapping hibernate".
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do know. And my B has only 1 reference to A. BUT I want to add something to the new class B so it'll be deleted when A is deleted. The cascade I talked about you add to the parent right?
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
logically if you don't add a reference of B in your class A then how could class A know that you B got deleted ? in order to delete B when A gets deleted is to add a reference of B in class A. and then do the mapping stuff.
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are confusing the classes.
I have old class A that I do not want to touch. I am creating new class B that one of the fields is A's key. I want to add something to the new B so it will be deleted when A is deleted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic