-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Foreign key relationship
PostPosted: Wed Aug 04, 2004 11:27 pm 
Newbie

Joined: Wed Aug 04, 2004 11:20 pm
Posts: 1
Using Hibernate as the persistence layer, I'm looking for a solution for the following scenario.

table A:
id int not null PK
field1 string null
field2 string null

table B:
id int not null PK
a_id int null
field1 string null
field2 string null

table B has a foreign key constraint attached to table A.id

table A data:
id field1 field2
1 string1 string2
2 string3 string4

table B data:
id a_id field1 field2
1 1 temp1 temp2
2 2 temp3 temp4
3 1 temp5 temp6

When table A with id=1 is deleted, how do I update table B with a_id=1 to a_id=null? I do not want to delete corresponding records of table B, I simply want to update the a_id values to "null".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 12:00 pm 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Map A->B association as cascade="save-update" and before deleting of A instance call some kind of detach method to unbind correponding children objects of class B:
Code:
A a = (A) session.load(A.class, id);
a.unattachChildren()
session.flush();
session.delete(a);
session.flush();


Interceptor or Lifecycle callback could help you to automate this. But I would prefer to put that code into DAO and use it everywhere I need this functionality.

--
Leonid


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.