Session.save updates data in sleep mode

I am trying to try the code to save student information in hibernate session.save (). In this, student name, class, teacher identifier.

Table: Student

SNO SNAME             SCLASS         TNO
----------- ----------------------------------------
1 J D Alex            3                1
2 Goaty               2                2
3 J D Paul            7                1

The 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 viewing the result, the class value is updated as 8 for student ID 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 does the updated student class value in the database? .Save mean insert data. But here the value is updated. Please let me know. If you have any problems?. If any question was sorry.

+3
source share
3 answers

This is the expected behavior of Hibernate.

, persistent . , . (.. Session.flush()) hibernate ( automatic dirty checking) SQL- , , , , JVM.

FlushMode. FlushMode.AUTO, , Session.flush() . , Session.flush(), - , UPDATE.

:

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();
+7

.

, get, . save , , . Hibernate , update.

+1

I think this is because you work with transactions. When you make a transaction, it automatically flushes any changes to the already loaded object, in this case your Student object.
It does not save the object, because it already has a unique identifier attached to the session.
I would think that Hibernate would throw an exception because you were trying to save an object that was already attached.

Hope this helps you.

0
source

All Articles