Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate - saveOrUpdate = insert or update?

 
Joshua White
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

is the hibernate saveOrUpdate method supposed to insert/update a hibernate object?

I have a new object that I am passing to saveOrUpdate, and I am not finding it in the DB.

Thanks,

Joshua
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should save if I recall correctly, any errors?
enable logging, sql queries as well, and post the output if it doesn't tell you enough.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will INSERT or UPDATE depending on the value of the objects identifier (PK if you will). If your transient object has an identifier value, then update() is called, if it doesn't save() is called (which generates an identifier). So your new object should be inserted, unless you have already assigned an identifier which isn't in the DB, in which case an update statement will be called on a record that doesn't yet exist - which won't work of course. Show us your code if you are still confused.
 
Santosh Maskar
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
this must be create/update a new record ,
There is two cases
1. if user is trying to create a new record then primary key will be automatically created for the table

then hibernate will create a new record

while creating the record it will check the primary key inside the table and then create a new value for the key

2. if user will try to update the record then hibernate will get the primary key and it will update hte record insted of creating a new one
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

did you close the session and commit your transaction ? If not the new record will not make it to the DB.



cheers

pascal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic