Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE 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
 
    Bookmark Topic Watch Topic
  • New Topic