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.  [ 2 posts ] 
Author Message
 Post subject: inverse attribute my undrstng crrct or not canany one tell ?
PostPosted: Wed Jul 23, 2008 10:46 am 
Newbie

Joined: Mon Jul 21, 2008 1:08 pm
Posts: 3
Location: INDIA
Hello everyone,

Iam new to hibernate.
I have doubt on inverse attribute.I read several articles on inverse attribute i understood it partially.But i have few doubts.So can any please clear my doubt.


Say for one to many association

With inverse=true at "one" side ,hibernate synchronizes the "many" side (foreign key coloumn) to database.So inverse avoids update statements.(why and how an update statement is generated?)

As far as i know about associations in Hibernate they are by default unidirectional.So in HIbernate say for example Parent to child has one to many relation ship.

So Hibernate reads Parent.hbm.xml and identifies that Parent has an association with Child.But Hibernate doesnot know that Child too has association with Parent.

Hibernate reads Child.hbm.xml and identifies that Child has an association with Parent.But Hibernate doesnot know that Parent too has association with Child.

(To overcome this problem we use inverse=true which makes the association bidirectional).

The association is formed with foreign key coloumn in child table that references to primary key of Parent table.

(AS given in Hibernate text books) Due to this,there are 2 different in memory representation of same foreign key coloumn:

1)Parent property of Child
2)Elements of child collection held by Parent.

So i understood it as Parent is communicating with collection of children through foreign key coloumn of child.
And child is communicating with Parent through the same foreign key coloumn of Child table.

So whenever we add child to parent and parent to child.

Hibernate detects 2 changes in the in memory persistent instances.But in database there is only one foreign key coloumn in child table.So hibernate generates update statements.

Here my doubt is why 2 update statements(as given in many Hibernate textbooks)?And what is the meaning of this update statement.(I think that update statement indicates that it forms association with child)

But What i think is "when parent communicate with child, parent insert a number in to foreign key coloumn of child table( which is a insert statement) and when child communicates with parent ,child just updates the value(which is same) of foreign key coloumn.So it should be insert and update statement.(Because an update statement could occur only when there was a previous value)But not 2 update statements as given in hibernate text books

But when we set show_sql property to true i can see only one update statement(if there is one record each in parent and child).

Here iam not clear about this scenario of update statements. Can any one explain?

Also say in Parent.hbm.xml we "havent" set invese=true and cascade=all.

And as part of code we have set parent to child and child to parent.
And we just use session.save(parent).

So if we see output we could see a insert statement into parent table and one update statement(I think update statement indicates that it forms association with child)updating foreign key coloumn in child table.

But why update statement why cant it be insert?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 3:36 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
So if we see output we could see a insert statement into parent table and one update statement(I think update statement indicates that it forms association with child)updating foreign key coloumn in child table.


I think that's precisely what's happening.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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