File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate - saveOrUpdate = insert or update? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate - saveOrUpdate = insert or update?" Watch "Hibernate - saveOrUpdate = insert or update?" New topic

Hibernate - saveOrUpdate = insert or update?

Joshua White
Ranch Hand

Joined: Jun 04, 2001
Posts: 97
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.


Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
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

Joined: Apr 14, 2004
Posts: 10336

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.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Santosh Maskar
Ranch Hand

Joined: Jul 02, 2003
Posts: 226
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

Joined: Jun 19, 2001
Posts: 547

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


I agree. Here's the link:
subject: Hibernate - saveOrUpdate = insert or update?
It's not a secret anymore!