hello every one
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 one please clear my doubt.
Say for one to many association which is bi directional
With inverse=true at one side ,hibernate synchronizes the "many" side foreign key coloumn to database.So inverse avoids update statements.
And hibernate understands that the association is bidirectional.
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.
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 generate 2 update statements.
Here my doubt is why 2 update statements(as given in many Hibernate textbooks)?
But What i think is when parent communicate with child ,parent insert a number in to foreign key coloumn and when child communicates with parent ,child just updates the previous value set by Parent.So it should be insert and update statement.
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?
So please can any one explain my doubts?
Last edited by kiran on Tue Jul 22, 2008 12:00 am, edited 4 times in total.
|