Monday, 11 March 2019

Hibernate - Difference between update() and merge() method

Hibernate - Difference between update() and merge() method:
============================================================

Some points of update() method:
------------------------------------
1. it returns void (means will NOT return any value)
2. It doesn't check the record in the database table.
3. It performs directly update query on the database - so if record doesn't exist then it throw the exception.

Limitations of update() method
---------------------------------
1. ONLY non-primary_key column we can update.
2. Complete record we need to update. It is NOT possible to update one column for one record.

For example:

Suppose we have studentdb2 table with some records:
s_roll name course fees contact
7901 Bhupender MCA 23000 9891297115
7902 Ram Kumar BCA 12000 1223344567

Bean class:
public class Student {
private int roll;
private String name, course, contact;
private float fees;
//getter & setter methods
}

Now with above scenarios if we use update method like:

PROBLEM 1:
==========================
Student st=new Student();
st.setRoll(7901);
st.setName("Bhupender Dagar");
ses.update(st);//here ses is the Session object here.

Note: At this moment what will happen - course, fees and contact column value will be set to null.
After this operation studentdb2 table will look like this:
s_roll name            course fees contact
7901 Bhupender Dagar null         null null
7902 Ram Kumar BCA 12000 1223344567

So, be take care to perform update() method. "Complete record we need to update. It is NOT possible to update one column for one record"

PROBLEM 2:
==========================
If I use update() method with get() or load() with same record then it thow the exception. i.e duplicate object
associate with the same session.
For example:

/*
Below line record will fetch data from studentdb2 table and create same clone copy with bean class object and save this object in Session object.
*/
Student st=(Student)ses.get(7901);

Now after this if I perform update method like:
Student st1=new Student();
st1.setRoll(7901);
st1.setName("Bhupender Dagar");
ses.update(st1);//At this line an exception will occur because already this object present in
//session object.
//At this moment HB will try to save st1 in Session object and will find already same object
//there. So duplicate object find exception throw at run time.


Note: At this moment we can use merge() method:
ses.merge(st1);//At this line HB will merge the st1 to st in Session and then perform the update //operaion successfully. SO NO any exception will throw at this moment.




PROBLEM 1 - with merge() method:
============================
Student st=new Student();
st.setRoll(7901);
st.setName("Bhupender Dagar");
ses.merge(st);//here ses is the Session object here.

Note: At this moment what will happen - course, fees and contact column value will be set to null.
After this operation studentdb2 table will look like this:
s_roll name             course fees contact
7901 Bhupender Dagar null         null null
7902 Ram Kumar BCA 12000 1223344567

So, be take care to perform merge() method. "Complete record we need to update. It is NOT possible to update one column for one record"
So at this moment merge() and update() will set null values.


Some points of merge() method:
--------------------------------------------------------------------------
1. it returns void value.
2. Before update the record it checks the record in the database table.
3. If record find and data is different - then it perform update query.
4. If record find and data are same - then it will NOT perform update query.
5. You can use merge() method with get() and load() with same record.

No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...