• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic