-->
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.  [ 8 posts ] 
Author Message
 Post subject: Problem updating/deleting many-to-one relationships
PostPosted: Mon Apr 19, 2004 3:55 am 
Newbie

Joined: Mon Jan 12, 2004 10:06 am
Posts: 15
Hi all and thanks in advance for your help.

I dont know if I am doing something bad or simply I think that Hibernate can do things that are not possible. I will try to explain what I am doing:

I have one relation (many-to-one) between two tables. My mappings are:

FileContent.hbm.xml
<set name="documents" lazy="false"
inverse="true"
cascade="all"
sort="unsorted">
<key column="bs_basecontent" />
<one-to-many class="com.Document" />
</set>

Document.hbm.xml
<many-to-one name="fileContent"
class="com.FileContent" cascade="none"
outer-join="auto"
update="true" insert="true" column="bs_basecontent"
not-null="true" />


Creating new objects have no problems, all works fine. Suposse that I create one FileContent with 5 documents inside it. Now the user wants to update the FileContent object and want only three documents (three updates and two deletes) so from the UI I obtain one FileContent with three Documents inside and try to make an update. All works fine but the two objects that are not present in the document's Set are not deleted.
I dont know if this is the normal behaviour (I have to delete this objects by hand) or if there is one way to make this operation (in a many-to-many relation during an update Hibernate update objects that are in the Set and delete the objects that are not present).

Any idea or suggestion? Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 4:11 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
Read chapter 9 of the manual. cascade="all-delete-orphan" is probably what you want.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 4:45 am 
Newbie

Joined: Mon Jan 12, 2004 10:06 am
Posts: 15
Hi again.

I forgot to say in my previous post that using cascade="all-delete-orphan" dont change anything, I mean, Hibernate update elements present in the Set but dont delete the elements that are no present in it.
The first thing that I want to know is If Hibernate can delete objects that are not present in my set (so I am doing something bad) or if I have to delete child objects by hand.

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 4:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This works with cascade="all-delete-orphan". There must be an error in your code somewhere.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 7:11 am 
Newbie

Joined: Mon Jan 12, 2004 10:06 am
Posts: 15
Ok. This is what I am doing:

From my UI I have user's information. With this:

// Open Session
// Open Transaction


FileContent fileContent = new FileContent();
fileContent.setId("1");
fileContent.setTitle("My new Title");
// Other ...

Document contentDocs1 = new Document();
contentDocs1.setId("1");
contentDocs1.setOrder("100");
contentDocs1.setBasicContent(fileContent);

Set documents = new HashSet();
documents.add(contentDocs1);
fileContent.setDocuments(documents);

// Save
manager.saveFileContent(fileContent);

// Close Transaction
// Close Session

with this code, FileContent is updated, Document is updated but Documents not referenced in the Set are not deleted (with cascade="all-delete-orphan" and hibernate 2.1.1).

Any ideas? Thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 7:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You must not exchange the Set loaded by Hibernat with a newly constructed HashSet if you want all-delete-orphan to work.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 9:36 am 
Newbie

Joined: Mon Jan 12, 2004 10:06 am
Posts: 15
Hi again and thanks for help me.

First of all, I'm using one session to load data, then send them to user (and close session) and in a new session I'm trying to update data.

So if I understand you, I have to use the same Set loaded with Hibernate to save/update/delete the objects (in my many-to-one), so I can't create one new DAO object which contains the modified data received from UI (with the same ID as the original) and saveorupdate() it, I have to load the old data and in this object make changes with values that I have received from user and after that saveorupdate? Because I have no problem to make changes to one object alone (I dont need to load the object first), I have problems with relations.

I know those are very beginner problems ... thanks again for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 10:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
all-delete-orphan only works when you modify sets created by Hibernate, because the set is used to track changes.


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