-->
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.  [ 4 posts ] 
Author Message
 Post subject: mapping problem with some parent/child relations
PostPosted: Tue Oct 18, 2005 8:05 am 
Newbie

Joined: Tue Oct 18, 2005 7:40 am
Posts: 2
Hi all,
First of all, thanks in advance.
I'll try to explain my problem the simplest and best I can.
I'm using Hibernate 2.
I have one entity: Image, that can, or not, belong to another entity: Cd (of images).
I map this relation this way:

<hibernate-mapping>
<class name="....model.Image" table="IMAGES" dynamic-update="false" dynamic-insert="false">
...
<many-to-one name="cd" class="....model.CD" cascade="none" outer-join="auto" update="true" insert="true" column="CD_ID" />
...
</class>
</hibernate-mapping>

I assign then an image to a cd: image.setCD(new CD("MyImages"))
And then delete the cd.
How can I map the relation to, when I delete the CD, the image returns to its original state, with no cd assigned?
It's not the tipical relation that when deleting the parent, all the childs are deleted too, i need to keep the child orphan (with a null CD).

If this question, or a similar, have been made before, I'm sorry but I could not find it.

Thanks all for your time.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 5:26 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
You need to think about which side of the relationship you're going to manage. If you do image.setCD(cd) then you're managing the image side of the relationship. If you implement this with the proper FK constraints, you won't be able to delete a CD, and it certainly won't update any of the images if you do.

You said in your post "I assign then an image to a cd:" which leads me to think that you're implementing this in the opposite way you've imagined it. Maybe what you need is to manage the set of images on each CD, using a one-to-many. This may make things clearer.

-Chris


Top
 Profile  
 
 Post subject: Re: mapping problem with some parent/child relations
PostPosted: Tue Oct 18, 2005 7:52 pm 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
graimon wrote:
How can I map the relation to, when I delete the CD, the image returns to its original state, with no cd assigned?
It's not the tipical relation that when deleting the parent, all the childs are deleted too, i need to keep the child orphan (with a null CD).


Have you tried setting an Image's parent to NULL before deleting the CD?

For example:
Code:
  Session session = ...
  CD cd = (CD) session.load(...);
  Image im = (Image) session.load(...);
 
  im.setCD(null);
 
  session.saveOrUpdate(im);
  session.delete(cd);



Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 4:13 am 
Newbie

Joined: Tue Oct 18, 2005 7:40 am
Posts: 2
cbredesen:
I used to have the relation the other way, from cd to image, but because of the complexitiy of the application, I had to remove it. I guess I should have to restore the relationship as you say, but that would bring me another speed problems.

jd001982:
I allready had that solution in mind, but was my last chance, because I have several realtions like that, and I was trying to avoid making a delete function for each relation.

What I've done to fix this problem is use the Foreign Keys in the database so that they do the job for me, I know its not recommended, now I'm testing it, and I hope it works just fine.

Thanks all.


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