Three states of Hibernate object

hibernate has three states: Transient, persistent, detached The following figure shows the state transition of objects in hibernate: Example 1 @Test ...

hibernate has three states:
Transient, persistent, detached
The following figure shows the state transition of objects in hibernate:

Example 1

@Test public void Test12() { Configuration cfg = new Configuration().configure();//Get configuration SessionFactory sessionFactory = cfg.buildSessionFactory();//Create session factory Session session = sessionFactory.openSession();//Open session Transaction transaction = session.beginTransaction();//Open transaction Dept dept = new Dept(0, "gz", "123");//Object just got out by new, instantaneous state try { session.save(dept);//Convert to persistent state through save method SessionDao.transaction.commit();//Commit transaction, transition to free state }catch(Exception e) { e.printStackTrace(); SessionDao.transaction.rollback();//Rollback if transaction exception }finally{//close resource session.close(); sessionFactory.close(); } }

Execution result:

Hibernate: insert into dept (dname, loc, deptno) values (?, ?, ?)

You can see hibernate submits an insert statement
Example two:

Dept dept = new Dept(60, "gz", "123");//Object just got out by new, instantaneous state try { session.save(dept);//Convert to persistent state through save method dept.setDname("gz1");//First, compare whether the dept changes. If so, automatically save the dName to the persistent object transaction.commit();//Commit transaction, transition to free state dept.setDname("gz2"); session.save(dept);//No commit, invalid statement }

Execution result:

Hibernate: insert into dept (dname, loc, deptno) values (?, ?, ?) Hibernate: update dept set dname=?, loc=? where deptno=?

hibernate insert and update gz1

You can see that the invalid statement was not committed.
If an object and its persistent state are changed, hibernate will not send sql statements when making various modifications to the object or calling the update and save methods multiple times. Only when the thing is submitted, hibernate will compare the current object with the persistent object saved in the previous session. If they are not the same, it will send an update sq L statement, otherwise the update statement will not be sent
Example three:

Dept dept = new Dept(60, "gz", "123");//Object just got out by new, instantaneous state try { session.save(dept);//Convert to persistent state through save method dept.setDname("gz1"); session.save(dept); dept.setDname("gz2"); session.save(dept); transaction.commit();//Commit transaction, transition to free state }

How many more sql will be submitted this time?
The answer is: 1 (first save) + 1 (commit times)
When the session calls the load and get methods, if the object exists in the database, the object becomes a persistent object and is managed by the session.
However, no matter how variable the dept is, if there is no data change in the dept, the session has no response. If there is, only the dept in the session (persistence) will be changed. Only the commit of the transaction will compare before sending the update SQL.

3 December 2019, 17:08 | Views: 8788

Add new comment

For adding a comment, please log in
or create account

0 comments