wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Why wrong object is persisted first, when it is not saved first Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Why wrong object is persisted first, when it is not saved first" Watch "Why wrong object is persisted first, when it is not saved first" New topic
Author

Why wrong object is persisted first, when it is not saved first

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 516
    
    2

Hello All,

The subject line which I used is some what ambiguous and I will try to explain my question more clearly in the body below.

I'm trying a very simple Many to One bidirectional association between Students and Address. Many Students stay in One Address.

My mapping files are as below

Student.hbm.xml


Address.hbm.xml


Now in my coding,

I'm seeing this expected result, when I persist Address object as below


The hibernate logs are


From the above logs, I understand that since I'm persisting Address object, first an insert is made into Address table and then the associated tables.

But , if I persist Student instead of Address,
I'm seeing this expected result, when I persist Address object as below


The hibernate logs are


Even though I'm persisting Student first, why Address object is persisted first and then why an update is needed. What made this to happen? Is "inverse=true" on Many side the cause for this.

Please advice


Regards
KumarRaja

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

You are using cascade="all" on the many side of the relationship, I don't think that the right thing to do. Do you really want to persist an Address object when Student object is persisted or delete an Address object when Student object is deleted??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why wrong object is persisted first, when it is not saved first
 
Similar Threads
update query fired by hibernate!!!!
insert- update operation in hibernate
Hibernate Bidirectional Mapping Problem.
Having trouble in understanding bi directional relationships
Hibernate - How to do associations with join tables?