Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic