-->
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.  [ 5 posts ] 
Author Message
 Post subject: deleting a one-to-one child object
PostPosted: Fri Apr 20, 2007 6:33 am 
Newbie

Joined: Sat Nov 18, 2006 10:03 am
Posts: 3
Hi!
I have this little problem and it seems to me there are no posts in the forum about that.

I have a parent object A and a child object B mapped in two different database tables; in A's hibernate mapping I write:

<one-to-one name="a" class="A"
cascade="persist,save-update,delete" />

When I add B in A (calling a method like instanceOfA.setB(new B()), for example) B is correctly saved on the db, but when I set instanceOfA.setB(null) (because I want to remove the data related to the B object) and I save instaceOfA by calling hibernateSession.update(instaceOfA), the row on the db that corresponds to B are not deleted.

So the next time I retrieve instanceOfA from the database I have his B property filled with some values instead of the null I set previously.

Does anybody know how I can obtain the behaviour I want??
thanks in advance for any help.
A.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 8:14 am 
Newbie

Joined: Tue Aug 08, 2006 3:53 am
Posts: 17
Location: Tanznaia
Alex,

It would seem to me that you want to set your cascade options to include the delete-orphan option. This will (supposedly) remove any objects that become unattached to anything when the relationship is removed. You should be very careful that your 'child' object is not also referenced elsewhere when you use this though...

See chapter "10.11. Transitive persistence" in this link
http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html

If this becomes a problem (my project is a real mass of inter-relations) it is worth remembering that datastorage is normally less of a problem than speed of response (these days). Ask yourself - if the database ends up with a bunch of orphaned records in there - does it really matter? You could always write a cleanup procedure to run once a year that uses SQL to delete any true orphans.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 3:51 pm 
Newbie

Joined: Fri Apr 20, 2007 3:46 pm
Posts: 15
Hey Alex,

Hibernate will not automatically delete B even if you call a setter method and pass in the null.

delete-orphan options only works with one-to-many mapping but not with one-to-one.

Unfortunately you will have to manually call delete on B to remove it from db.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 22, 2007 9:30 am 
Newbie

Joined: Tue Aug 08, 2006 3:53 am
Posts: 17
Location: Tanznaia
Allex,

That's a goos point about one-to-one relationships.

If it really is one-to-one that you want, consider using a component for the second class - this effictively adds the class into the parent's table. It's a good mechanism for keeping your code nice and object-orientated without having the cost of a million database tables. It should also mean that your 'object B' gets deleted with the parent.

I hope this helps.

Ben.


Top
 Profile  
 
 Post subject: So is this a bug or a feature?
PostPosted: Fri May 18, 2007 11:29 am 
Newbie

Joined: Mon Oct 06, 2003 12:02 pm
Posts: 17
Hello,

Just ran into the same issue recently. I've got a "A" one-to-one (optional) "B" association. Only a small number of "A"'s have an associated "B". If a B exists, it has the same PK as A.

I was able to create a mapping in which instanceA.setB(new B()) would properly set B's PK, and insert it into the database.

However, like the original submitter, when I tried to delete B via instanceA.setB(null), the B record was not deleted from the database.

Anyone know if this behavior by design or is it a defect? I have found no hibernate documentation mentioning this.

Regards,
Dave

_________________
Dave


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.