-->
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: Saving Detached Object
PostPosted: Fri Apr 15, 2005 10:50 pm 
Newbie

Joined: Fri Apr 15, 2005 10:44 pm
Posts: 3
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1

Mapping documents:

ClientClass.java
<set name="clientClassNames" inverse="true" cascade="all-delete-orphan">
<key column="CLSSID"/>
<one-to-many class="com.hli.prism.classes.domain.ClientClassName"/>
</set>

ClientClassName.java
<many-to-one name="clientClass"
class="com.hli.prism.classes.domain.ClientClass"
column="CLSSID"
not-null="true"/>

Code between sessionFactory.openSession() and session.close():
session.saveOrUpdate(clientClass);

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Hypersonic

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

I have a parent-child relationship between ClientClass and ClientClassName with following declaration

Problem Faced
In my web layer, I fetch the data and display it on the screen. User unselects some of the ClientClassName rows and select some new ClientClassName rows. While saving, hibernate creates inserts rows for the new ClientClassName but does not delete the unselected ClientClassName dereferenced from the collection.

Appreciate your help to resolve this issue.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 16, 2005 1:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you have inverse="true" on your association and that means its not the collection that is "controlling" the relationship. It is the many-to-one that is in charge and thus you should (as always) remember to null out that many-to-one too.

Remember this is pure java and in java you need to manage your bidirectional relationships.

You could also look at cascade delete-orphan to see if that can help you.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 16, 2005 1:01 pm 
Newbie

Joined: Fri Apr 15, 2005 10:44 pm
Posts: 3
Thanks for your reply. I tried the following code

Lets take following example to understand this better
Database
1) I have 1 row in database for ClientClass with id = 1
2) I have 4 rows in database for ClientClassName with id 1,2,3 and 4 associated with ClientClass id 1.

In my detached ClientClass object, I have done following
1) Set Id as 1
2) Created a collection object containing ClientClassNames with Id 1,2, 4and 5.

Now in my data access layer I do the following
1) session.saveOrUpdate(clientClass); - This statement execute an update query for ClientClass, update queries for ClientClassName with id 1,2, and 4, and insert query for ClientClassName with id 5. NO DELETE QUERY for ClientClassName with Id 3.
2) Per your suggestion, I tried
ClientClassName clas1 = (ClientClassName) session.load(ClientClassName.class, new Long(3));
clas1.setClientClass(null);
session.saveOrUpdate(clas1);

But, it fired an update instead of delete. Now, I know I am doing something crazy over here. If I know the ClientClassName id that should be deleted then I should be executing session.delete(clas1). My question is where should I be setting classNameClient.setClientClass(null). Please let me know. Appreciate your help!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 17, 2005 2:11 am 
Newbie

Joined: Tue Apr 05, 2005 8:01 am
Posts: 12
I think doing something should help.

What you should do is take the ClinetName object from the session,

ClientClass clientClass = session.load(ClientClass.class,new Long(1));
for(int i=0; i<clientClass.getClientClassName().size(); i++){
ClientClassName clientClassName = clientClass.getClientClassName().get(i);
//find the matching condition
if(clientClassName.getIdx()==3){
clientClass.getClientClassName().remove(i);
return;
}
}

//here do the adding of new objects say 5.
clentClass.getClientClassName().add(new ClientClassName());

session.save(clientClass);

NOTE : cascade option should be set for delete.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.