• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why update statement issued in cascade = "save-update"

 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have implemented onw parent child relationship as follows :


And I am saving the object as :



Now I have a couple of questions :

1) When I ran the program I have got the following output :
Hibernate: select hibernate_sequence.nextval from dual
[8/19/07 12:22:55:204 IST] 00000041 SystemOut O TAD===> Setting id in Parent.setId 10
[8/19/07 12:22:55:244 IST] 00000041 SystemOut O TAD===> Getting Id in Child.getId null
[8/19/07 12:22:55:244 IST] 00000041 SystemOut O Hibernate: select hibernate_sequence.nextval from dual
[8/19/07 12:22:55:244 IST] 00000041 SystemOut O TAD===> Setting ID in Child.setId 11
[8/19/07 12:22:55:244 IST] 00000041 SystemOut O TAD===> Returning Id in Parent.getId 10
[8/19/07 12:22:55:254 IST] 00000041 SystemOut O TAD===> Getting Id in Child.getId 11
[8/19/07 12:22:55:254 IST] 00000041 SystemOut O Hibernate: insert into Parent (name, id) values (?, ?)
[8/19/07 12:22:55:275 IST] 00000041 SystemOut O Hibernate: insert into Child (name, parent_id, id) values (?, ?, ?)
[8/19/07 12:22:55:275 IST] 00000041 SystemOut O Hibernate: update Child set id=? where id=?
[8/19/07 12:22:55:285 IST] 00000041 SystemOut O TAD===> Returning Id in Parent.getId 10
[8/19/07 12:22:55:285 IST] 00000041 SystemOut O Parents' Id 10
[8/19/07 12:22:55:285 IST] 00000041 SystemOut O TAD===> Getting Id in Child.getId 11
[8/19/07 12:22:55:285 IST] 00000041 SystemOut O Child's ID 11

Why there is an extra update when child is already saved?

2) If I save the object as session.save(c1) , it still works i.e. the parent is also saved.Is it because in the mapping I have specified as cascade="save-update" in many-to-one?

Thanks
Tanu
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is most likely because you haven't marked one of the sides of the relationships as "inverse=true"

So Hibernate thinks that your mapping is two totally different relationship ammping, it does not no that the one-to-many mapping is mapping the exact same relationship as the many-to-one mapping.

Mark
 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic