-->
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: Updating parent deletes all the records in intermidiate tabl
PostPosted: Mon Apr 03, 2006 1:45 pm 
Newbie

Joined: Mon Apr 03, 2006 1:30 pm
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp


I have a mapping for parent child relation. Where Feed is one table and DependentContacts as another table. An inermidate table is formed with
Feed_Dependent_contact. Now When I try to update the feed table(other propery not dependentcontacts) I see that the rows corresponding to that feed entry is getting deleted in feed_dependent_contact table(intermidiate table). I have also given below the SQL executed. I find one statement that deletes from the intermidiate table..why is this ?

Can any one help me to solve this problem..

Thanks in advance..



Hibernate version:3.0.5

Mapping documents:
Feed.hbm.xml
<set name="dependentContactsSet" table="FEED_DEPENDENT_CONTACT" lazy="true" cascade="all,delete-orphan">
<key column="FEED_ID" update="false"></key>
<many-to-many class="model.DependentContact" />
</set>

DependentContact.hbm.xml
<set name="feeds" table="FEED_DEPENDENT_CONTACT" lazy="true" >
<key column="DEPENDENT_CONTACT_ID" update="true"></key>
<many-to-many column="FEED_ID" class="model.Feed" />
</set>

FeedDependentContact.hbm.xml
<composite-id name="comp_id" class="model.FeedDependentContactPK">
<key-property name="feedId" column="FEED_ID" />
<key-property name="dependentContactId" column="DEPENDENT_CONTACT_ID" />
</composite-id>






Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:MySQL4.1

The generated SQL (show_sql=true):
Hibernate: update feed set FEED_NAME=? where ID=?
Hibernate: delete from FEED_DEPENDENT_CONTACT where FEED_ID=?
Hibernate: select feed0_.ID as ID0_, feed0_.FEED_NAME as FEED2_2_0_ from feed feed0_ where feed0_.ID=?
Hibernate: select dependentc0_.FEED_ID as FEED1_1_, dependentc0_.elt as elt1_, dependentc1_.ID as ID0_, dependentc1_.DEPENDENT_CONTACT_NAME as DEPENDENT2_1_0_ from FEED_DEPENDENT_CONTACT dependentc0_ inner join dependent_contact dependentc1_ on dependentc0_.elt=dependentc1_.ID where dependentc0_.FEED_ID=?



Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 11:19 pm 
Newbie

Joined: Wed Oct 12, 2005 4:39 pm
Posts: 15
Location: India
Replace cascade="all,delete-orphan" to cascade="none" and you should see what you are expecting!


But then you will need to explicitly save your child items. Saving the parent only will not save the children.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 4:33 am 
Newbie

Joined: Mon Apr 03, 2006 1:30 pm
Posts: 6
rags_1979 wrote:
Replace cascade="all,delete-orphan" to cascade="none" and you should see what you are expecting!


But then you will need to explicitly save your child items. Saving the parent only will not save the children.


Thanks a lot !
Well modifying the cascade attribute as you metioned.

<set name="dependentContactsSet" table="FEED_DEPENDENT_CONTACT" lazy="true" cascade="none">
<key column="FEED_ID" update="false"></key>
<many-to-many class="model.DependentContact" />
</set>

is not solving the problem.I find the rows in intermideate table(FEED_DEPENDENT_CONTACT) are
deleted. Is there any thing that is missed out.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 5:01 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

Please post the code where you update the object. Maybe somebody will find a solution.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 5:11 am 
Newbie

Joined: Mon Apr 03, 2006 1:30 pm
Posts: 6
Hi,

The below is the code I use

public class BasicHibernateExampleTest extends HibernateDaoSupport {

public void testFeedUpdate(){

Feed feed = new Feed();
feed.setFeedName("MyFeedName");
feed.setFeedId(1);
getHibernateTemplate().update(feed);

}



Feed id with 1 is already in the FEED table and also There is some rows in FEED_DEPENDENT_CONTACT Table too .

after this method I find the FEED table is updated and all rows in FEED_DEPENDENT_CONTACT is deleted for the feed with id 1.

Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 8:12 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

I never used HibernateDaoSupport, but:

- it's a very normal behavior to delete your rows from intermediate table. Why ? You are not updating the old object, you just put a similar one instead.
- you should use something like/similar:
Feed feed = getHibernateTemplate().load(Feed.class, new Long/Integer(1)); //find API and check how an onject can be loaded using HibernateDaoSupport
feed.setFeedName("MyFeedName");
// feed.setFeedId(1); no longer needed
getHibernateTemplate().update(feed);

Also:
- read about: transient, detached, persistent object (see Hibernate in Action) - I'm sure that after reading those chapters you will smile when you will read again your question
- read about parent/child uni/bidirectional relation (see Hibernate in Action). This will clarify what is the purpose of cascade and how hibernate manage the relations.

If more help is needed ask.

Best.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 8:58 am 
Newbie

Joined: Mon Apr 03, 2006 1:30 pm
Posts: 6
Hi,

Thanks a lot !


I have used the same way you have suggested. First I load the object and then I update it. But does that seam correct ? Bcos I update only the parent record. If this is going to be normal SQL the query will be UPDATE .... WHERE FEED_ID = 1( One single query).

But here in our code this fire two query one is fetch and other is update. Is this really nessarray ? or any other alternative.

Can we update the parent object only through the detached object alone?.



Thanks !!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 10:23 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi again,

Wowowo. Or better said "hold on".

Can we agree that a program/application can be written inside only one famous method? (public static void main). Why do we choose to encapsulate, agregate, inherit, use abstraction, build polymorphic objects? Because this is OOP. Even more, some people spend many time writing books about how to "think" in a OOP environment. Even myself, when writing this reply I'm thinking and write directly in enghish and not in my mother language.

You are saying that direct SQL is more efficient (one query instead of two). Very true. But my question is: why did you choose to use Hibernate? Why not JDBC ?

So,

- is it necesary beacause this is the way hibernate is thinking the update.
- another alternative is to get the hibernate session and execute the SQL statement
- in you code (method testFeedUpdate) I don't see any detached object. You have only one transient object.
- a detached object is obtained using one query to fetch it and if you update it you've just executed the second query. Congratulations, you've just started to think in hibernate.

Hope you don't find the post as an aggresive one. Anyway, this is not the intention.

And if the information helped you please also rate the answers.


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.