Hibernate 3-state

Source: Internet
Author: User

Hibernate 3-state
I. Overview (I) Instantaneous State (temporary state)

In an object, if the object has just been created but is not persistent, It is a transient state.

Features:

(1) not associated with the Session instance

(2) There is no record associated with the instantaneous object in the database.

(2) Persistence

A persistent object is an object that has been saved to the database, and the object is still in the Hibernate Session cache management. Any modification to the object will be synchronized to the database when the cache is cleared.

Features:

(1) A persistent instance has a corresponding record in the database and has a persistent identifier (identifier ).

(2) objects associated with sessions

(3) free status (hosting status and offline status)

When a persistent object is removed from the Cache Management of Hibernate, it is in the Free State. The biggest difference between a free object and a free object is that, the free object may still have a record corresponding to it in the database, but now the free object is out of the Cache Management of Hibernate, free objects do not have data records corresponding to them in the database.

Features:

(1) essentially the same as the instantaneous object

(2) Only one database record id is missing from the instantaneous object.

Ii. Conversion Diagram

Through the following conversion chart, we can understand how the three states are converted. If we print this picture in our own mind, the three States are nothing sacred!


Iii. Practice

The above is an explanation of the three-state theoretical knowledge, so next we will try them out from the actual code!

1. TestTransient

Session = HibernateUtil. openSession (); session. beginTransaction (); User user = new User (); user. setUsername ("aaa"); user. setPassword ("aaa"); user. setBorn (new Date ();/** the above user is a Transient (instantaneous). At this time, the user is not managed by the session, that is, the * cache of the session does not contain the user object. After the save method is executed, the user is hosted by the session, in addition, if this object * user exists in the database, it becomes a Persistent (persistence object) */session. save (user); session. getTransaction (). commit ();

At this point, we know that hibernate will issue an insert statement. After the save method is executed, the user object becomes a persistent object.


Hibernate: insertinto t_user (born, password, username) values (?, ?, ?)

2. TestPersistent01

     

Session = HibernateUtil. openSession (); session. beginTransaction (); User user = new User (); user. setUsername ("aaa"); user. setPassword ("aaa"); user. setBorn (new Date (); // The above u is Transient (Instantaneous State), indicating that it is not managed by the session and the database does not have // It is managed by the session after saving is executed, in addition, the database already exists, and the session is in the Persistent state. save (user); // at this time, u is in the persistent state and has been managed by the session. When submitting, compare the object in the session with the current object // if the values of the two objects are inconsistent, the corresponding SQL statement user will continue to be issued. setPassword ("bbb"); // at this time, two SQL statements are sent, one for the user to insert, and the other for the update session. getTransaction (). commit ();

After the save method is called, the user is already a Persistent Object and is saved in the session cache. Then, the user modifies the attribute value again. When the transaction is committed, in this case, the hibernate object compares the current user object with the user object in the session cache. If the two objects are the same, the update statement is not sent. Otherwise, if the two objects are different, the update statement is issued.

Hibernate: insertinto t_user (born, password, username) values (?, ?, ?)Hibernate: updatet_user set born=?, password=?, username=? where id=?

3. TestPersistent02

     

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); session = HibernateUtil. openSession (); session. beginTransaction (); User u = new User (); u. setBorn (new Date (); u. setUsername ("zhangsan"); u. setPassword ("zhangsan"); session. save (u); u. setPassword ("222"); // This statement has no meaning for session. save (u); u. setPassword ("zhangsan111"); // meaningless session. update (u); u. setBorn (sdf. parse ("1988-12-22"); // meaningless session. update (u); session. getTransaction (). commit ();

How many SQL statements will be issued at this time? In the same way, after calling the save method, u is already a Persistent Object. Remember: if an object and its status are persistent, modify the object, or hibernate does not send SQL statements when calling multiple update and save methods. Only when a transaction is submitted, at this time, hibernate will compare the current object with the Persistent Object stored in the session. If they are different, an update SQL statement will be sent. Otherwise, no update statement will be sent.

Hibernate: insertinto t_user (born, password, username) values (?, ?, ?)Hibernate: updatet_user set born=?, password=?, username=? where id=?

4. TestPersistent03

      

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); session = HibernateUtil. openSession (); session. beginTransaction (); User u = new User (); u. setBorn (sdf. parse ("1976-2-3"); u. setUsername ("zhangsan2"); u. setPassword ("zhangsan2"); session. save (u);/** the following three statements do not make any sense */session. save (u); session. update (u); session. update (u); u. setUsername ("zhangsan3"); session. getTransaction (). commit ();

Believe this test case, everyone should know the result. That's right. At this time, hibernate will also issue two SQL statements in the same principle.

Hibernate: insertinto t_user (born, password, username) values (?, ?, ?)Hibernate: updatet_user set born=?, password=?, username=? where id=?

5. TestPersistent04

     

Session = HibernateUtil. openSession (); session. beginTransaction (); // at this time, u is the Persistent User u = (User) session. load (User. class, 4); // because the u object is inconsistent with the object in the session, the SQL statement is sent to complete the u update. setUsername ("bbb"); session. getTransaction (). commit ();

Let's see how many SQL statements will be issued at this time? Remember: when the session calls the load and get methods, if this object exists in the database, the object becomes a Persistent Object and is hosted by the session. Therefore, if an object is operated at this time, the transaction will be committed and compared with the persistent object in the session. Therefore, two SQL statements will be sent here.

Hibernate: selectuser0_.id as id0_0_, user0_.born as born0_0_, user0_.password as password0_0_,user0_.username as username0_0_ from t_user user0_ where user0_.id=?Hibernate: updatet_user set born=?, password=?, username=? where id=?

6. TestPersistent05

     

Session = HibernateUtil. openSession (); session. beginTransaction (); // at this time, u is the Persistent User u = (User) session. load (User. class, 4); u. setUsername ("123"); // clears the session. clear (); session. getTransaction (). commit ();

Let's look at this example again. When we load the user object, the user is a persistent object and this object exists in the session cache. Now, after modifying the user, we call the session. clear () method. At this time, the cache object of the session will be cleared, so there will be no user object in the session. At this time, when the transaction is committed, this object is no longer in the session, so no operation is performed. Therefore, only one select statement is sent here.

Hibernate: selectuser0_.id as id0_0_, user0_.born as born0_0_, user0_.password as password0_0_,user0_.username as username0_0_ from t_user user0_ where user0_.id=?

7. TestDetached01

      

Session = HibernateUtil. openSession (); session. beginTransaction (); // at this time, u is an offline object and is not hosted by the session User u = new User (); u. setId (4); u. setPassword ("hahahaha"); // when saving is executed, a piece of data is always added. At this time, the id will generate the session according to the rules defined by Hibernate. save (u); session. getTransaction (). commit ();

We can see that when u. setId (4), at this time u is an offline object, because the database has this object id = 4, but this object is not managed by the session, so this object is an offline object. What method should I call to make the offline object A Persistent object? We know that you can call the save method to change an object into a persistent object. However, when save is executed, hibernate inserts a data record into the database based on the id generation policy, therefore, if you call the save method, the database will send an insert statement:

Hibernate: insertinto t_user (born, password, username) values (?, ?, ?)

Therefore, for offline objects to be converted into persistent objects, we cannot use the save method, but should use the update method.

8. TestDetached02

     

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); session = HibernateUtil. openSession (); session. beginTransaction (); User u = new User (); u. setId (5); // After the update is completed, the session will also become persistent. update (u); u. setBorn (sdf. parse ("1998-12-22"); u. setPassword ("world"); u. setUsername ("world"); // an SQL session is generated. update (u); session. getTransaction (). commit ();

Now we can see that after the update method is called, u has become a persistent object. If the u object is modified at this time, when the transaction is committed, the object is compared with the persistence object just saved in the session. If it is different, an SQL statement is sent.

Hibernate: updatet_user set born=?, password=?, username=? where id=?

9. TestDetached03

      

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); session = HibernateUtil. openSession (); session. beginTransaction (); User u = new User (); u. setId (5); // After the update is completed, the session will also become persistent. update (u); u. setBorn (sdf. parse ("1998-12-22"); u. setPassword ("lisi"); u. setUsername ("lisi"); // throw an exception u. setId (333); session. getTransaction (). commit ();

Let's take a look at this example. The previous operation is the same. After the update method is called, the user becomes a Persistent Object. After some modifications are made to the user, the user passes u again. when the setId (333) method sets the u ID, hibernate will report an error at this time because our u is already a persistent object, if you try to modify the value of the ID of a persistent object, an exception is thrown.

org.hibernate.HibernateException:identifier of an instance of com.xiaoluo.bean.User was altered from 5 to 333

10. TestDetached04

     

Session = HibernateUtil. openSession (); session. beginTransaction (); User u = new User (); u. setId (5); // now u is the transient object session. delete (u); // at this time, u is an instantaneous object and will not be managed by the session and database. setPassword ("wangwu"); session. getTransaction (). commit ();

Next let's take a look at this example. Here we call the session. after the delete () method, the u will become an instantaneous object, because this object does not exist in the Database. Since u is already an instantaneous object, hibernate will not send any modification statements for u after various modifications, so here there will be only one delete statement:

Hibernate: deletefrom t_user where id=?

11. TestDetached05

     

Session = HibernateUtil. openSession (); session. beginTransaction (); User u = new User (); u. setId (4); u. setPassword ("zhaoliu"); // if u is offline, update is executed. If it is instantaneous, Save is executed. // However, note: This method is not commonly used for sessions. saveOrUpdate (u); session. getTransaction (). commit ();

Here, let's take a look at the saveOrUpdate method. This method is actually a "lazy" method. If the object is an offline object, the update method is actually called after the method is executed, if the object is an instantaneous object, the save method is called. Remember: if the object has an ID value, such as u. setId (4), the object is assumed as an offline object, and the update operation is executed.

Hibernate: updatet_user set born=?, password=?, username=? where id=?

If I comment out the u. setId (4) sentence, then u is an instantaneous object, then the save operation will be executed and an insert statement will be sent.

Hibernate: insertinto t_user (born, password, username) values (?, ?, ?)


12. TestDetached06

     

Session = HibernateUtil. openSession (); session. beginTransaction (); // u1 is already in the persistent State User u1 = (User) session. load (User. class, 3); System. out. println (u1.getUsername (); // u2 is offline. User u2 = new User (); u2.setId (3); u2.setPassword ("123456789 "); // at this time, u2 will become a persistent state, and there will be two identical objects in the session cache. There cannot be two copies in the session; otherwise, an exception session will be thrown. saveOrUpdate (u2 );

Let's take a look at this example. At this time, our u1 is already a Persistent Object and saved in the session cache. by calling the saveOrUpdate method, u2 becomes a persistent object, at this time, it will also be stored in the session cache. At this time, there will be a persistent object in the session cache with two reference copies. At this time, hibernate will report an error.

org.hibernate.NonUniqueObjectException:a different object with the same identifier value was already associated withthe session: [com.xiaoluo.bean.User#3]

A session cannot have dual copy for a Persistent Object. To solve this problem, we need to introduce another session method, merge, the function of this method is to solve the problem of two-minute copy of a persistent object. This method combines the two objects into one object.

      

Session = HibernateUtil. openSession (); session. beginTransaction (); // u1 is already in the persistent State User u1 = (User) session. load (User. class, 3); System. out. println (u1.getUsername (); // u2 is offline. User u2 = new User (); u2.setId (3); u2.setPassword ("123456789 "); // at this time, u2 will become a persistent State. There are two identical objects in the session cache, and there cannot be two copies in the session, otherwise, an exception is thrown. // session. saveOrUpdate (u2); // The merge method checks whether the same object already exists in the session. If so, the two objects are merged into the session. merge (u2); // Best Practice: merge generally does not need a session. getTransaction (). commit ();

We can see that after the merge method is called, two persistent objects in the session will be merged into one object, but the merge method is not recommended.

Hibernate: selectuser0_.id as id0_0_, user0_.born as born0_0_, user0_.password as password0_0_,user0_.username as username0_0_ from t_user user0_ where user0_.id=?zhangsanHibernate: updatet_user set born=?, password=?, username=? where id=? 

Iv. Summary

Hibernate's three-state instantaneous, persistent, and free states are the foundation for learning Hibernate. Only by figuring out the conversion between them can we take full advantage of them in practice. This is just the beginning. Let's look forward to the next great event!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.