File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes save(), update, saveOrUpdate(), merge() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "save(), update, saveOrUpdate(), merge() ?" Watch "save(), update, saveOrUpdate(), merge() ?" New topic
Author

save(), update, saveOrUpdate(), merge() ?

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
I want to do an update function as follows:

Given a table STUDENT(ID, name, address, phone), ID is the PK using oracle sequence id, meanwhile I put "name" and "address" as "unique constraint" for business logics reason, so I don't have two rows with identical "name' and "address" either.

I want to update an existing row. I have tried


/** create a new object. In the table there is already a row that has the same "name" and "address" values as this new object, but they have different "phone" value. so this is an update (use same ID). or actually I don't care if it is a "delete" followed by a new "insert" (get a new ID though).
**/

Student student = new Student();
student.set.... //
..

1) try {
session().beginTransaction();
session.delete(row);
session.save(row);
// some other activity in this session
commit();
} catch(..)

2) try {
session.beginTransaction();
session.update(student);
// some other activity in this session
commit();
} catch(..)
3. try {
session.beginTransaction();
session.saveOrUpdate(student);
// some other activity in this session
commit();
} catch(..)
4. try {
session.beginTransaction();
session.merge(student);
// some other activity in this session
commit();
} catch(..)

I don't know the subtle difference among them. I want to put this activity and some other activities in ONE transaction (which forces them to use one same session I guess). So, if that's the requirement, which of above 4 snippets suit my need best ?

Thanks.
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
I believe three or four.

My understanding is that you'd want to use merge if your student object was, at point, involved in a hibernate session, became detached, was modified and now you want to save its changes. This is most often, but not exclusively, the case if you stuck the student object in the (web) session between web requests for instance (and thus need to associate it with a new (hibernate) session).

If you're producing the student object via new and then want to either insert if new, or update is existing use the saveOrUpdate call.
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Hi, here is what I have found after I posted the question -- First I create a new "Student" object, but since the new student has the same "name" and "address" as an existing one, I can't simply directly save or update using this new object, it gave me "identifier" error. So what I did was I retrieve the existing row that has the same "name" and "address" and use this object as a basis, modify this object by setting new "phone" value, and do a "update" using this original and modified object. Then it works. Does this make sense ?
 
jQuery in Action, 2nd edition
 
subject: save(), update, saveOrUpdate(), merge() ?