• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

set null to fk when object deleted

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working with JBOSS4.2 + JPA

Hey,

Lest assume the following:

Person has onetoone (cascade=remove) with Address
GirlFriend has onetoone (there is no cascade) with Address

i want that when i delete person it will delete address and nullify all element that refering to this address.

i add a predelete callback on adress and try to nullfy the refering element, but i get exception - flush during cascade is dangeroues, what can i do?

Thank you
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue has to do with cascade options. In Hibernate there is a delete-orphan which would do what you are looking for.

Mark
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Delete orphan delete object that has no reference to it.
Why it will help here?

You delete Person which cause to delete of Address, if you dont nullify the reference from GirlFriend to this Address you will get constraints violation.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you need the "delete" cascade option for Address to Girlfriend in order to get the fk nulled out in GirlFriend.

Or in your code you will have to go the long way and before you delete a Person, get the Address, from there get the GirlFriend, remove the association of Address from the GirlFriend object, update the GirlFriend object, then delete the Person.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic