posted 16 years ago
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.