Session.save is updating the data in hibernate
hibernate
Solution
This is the expected behaviour of Hibernate.
When a record is loaded by a hibernate session , its instance will be in the persistent state and managed by this session. If the values of the persistent instance are changed , they are considered as dirty . During the flushing process ( ie. `Session.flush()`) , hibernate will find out all the dirty instances (We call this process `automatic dirty checking` ) and generate and issue the necessary SQLs for them to update the corresponding DB records to make sure that the DB records will have the same state as the corresponding instances held in JVM.
The flushing behaviour of a hibernate session is determined by the `FlushMode` . By default , it is the `FlushMode.AUTO` which means that `session.flush()` will be invoked automatically before committing an transaction or execution of the query. So in your code , though you do not call `session.flush()` explicitly , the flushing process still occurs to issue these UPDATE statements.
Some remarks about the code:
Transaction tx1=session1.beginTransaction();
/**
* o2 is the in the persistent state and managed by session1
*/
Object o2=session1.get(Student.class,new Integer(3));
/**
*As the value of o2 is changed , it becomes dirty and hibernate will issue an UPDATE SQL
*for it during flushing.
*/
((Student)o2).setSclass("8");
/**
* save() only has effect on the transient instance. Nothing will
* be done when calling it on the persistent instance . So removing this line of code
* still produces the same result.
*/
session1.save(o2);
log.info("loadStdYearlyInfo:class "+((Student)o2).getSclass());
/**
*I believe default FlushMode (FlushMode.AUTO) is used in here ,so session.flush() will be invoked implicitly before tx1.commit().
*Automatic dirty checking occurs and UPDATE SQL is generated and issued to the DB
*to update the dirty o2 instance
*/
tx1.commit();
session1.close();
Problem
Iam trying to sample code for saving student information in hibernate session.save().In that,student name,class,teacher id. Table:Student ``` SNO SNAME SCLASS TNO ----------- ---------------------------------------- 1 J D Alex 3 1 2 Goaty 2 2 3 J D Paul 7 1 ``` Code:- ``` Transaction tx1=session1.beginTransaction(); Object o2=session1.get(Student.class,new Integer(3)); ((Student)o2).setSclass("8"); session1.save(o2); log.info("loadStdYearlyInfo:class "+((Student)o2).getSclass()); tx1.commit(); session1.close(); ``` After saving the data and seen the output the class value is updated as 8 for student id is 3 ``` SNO SNAME SCLASS TNO ----------- ---------------------------------------- 1 J D Alex 3 1 2 Goaty 2 2 3 J D Paul 8 1 [07/May/2012:10:03:06] info ( 3500): CORE3282: stdout: Hibernate: /* load com.aims.beans.Student */ select student0_.sno as sno0_, student0_.sname as sname1_0_, student0_.sclass as sclass1_0_, student0_.tno as tno1_0_ from student student0_ where student0_.sno=? [07/May/2012:10:03:06] info ( 3500): CORE3282: stdout: loadStdYearlyInfo:class 8 [07/May/2012:10:03:06] info ( 3500): CORE3282: stdout: Hibernate: /* update com.aims.beans.Student */ update student set sname=?, sclass=?, tno=? where sno=? [07/May/2012:10:03:06] info ( 3500): CORE3282: stdout: loadStdYearlyInfo2 ``` How the updated the student class value in database?.save means inserting the data.But here value is updated.Please let me know.if any issues?.if any wrong the question sorry.