Hibernate three states

Source: Internet
Author: User
Tags emit throw exception

Anyone who has studied hibernate may know that Hibernate has three states, transient (transient state), persistent (persistent state), and detached (offline), and the big guys may also know the difference between the three. such as instantaneous state is just new out of an object, has not been saved to the database, the persistence state is already saved to the database, the offline state is the database, but the session does not exist in the object. But are we all clear about the special methods of Hibernate's session? Or whether you can see at a glance how many SQL statements a test case will emit after repeated calls to the session, such as the Save,update method? This essay will give you an answer, this essay will be a large number of test cases to cover up hibernate of these three states of change, I believe that after reading this essay you will have hibernate of the three states have a deeper understanding.

Well, no more nonsense, I believe we all know the meaning of these three states of hibernate, so let's start with a picture of our three-state journey into hibernate.

1.TestTransient

Session = Hibernateutil.opensession ();            Session.begintransaction ();            User user = new user ();            User.setusername ("AAA");            User.setpassword ("AAA");            User.setborn (New Date ());             /*  above user is a transient (instantaneous state), at this time user is not managed by the session, that is, in the session of the             *  cache does not exist in the user this object, when the Save method is finished, At this point the user is hosted by the session, and the object exists in the database             *  user becomes a persistent (persistent object) */            session.save (user);            Session.gettransaction (). commit ();

At this point we know that hibernate emits an INSERT statement, and after the Save method is executed, the user object becomes a persisted object.

Hibernate:insert into 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 that there is no//execute save in the database, managed by the            session, and that the database already exists, and this is the persistent state            Session.save (user);            At this point you are persisted and have been managed by the session, and when committed, the object in the session is compared to the current object            ///If the values in the two objects are inconsistent, the corresponding SQL statement will continue to be issued            User.setpassword ("BBB");            This will emit 2 SQL, a user to do the insertion, one to do update            session.gettransaction (). commit ();

When the Save method is called, the user is already persisted, saved in the session cache, and the user modifies the value of the property, and when the transaction is committed, The Hibernate object then compares the current user object with the user object stored in the session cache, and if two objects are the same, the UPDATE statement is not sent, otherwise the UPDATE statement is issued if two objects are different.

Hibernate:insert into T_user (born, password, username) VALUES (?,?,?) Hibernate:update T_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");            The clause has no meaning            session.save (u);            U.setpassword ("zhangsan111");            No meaning            session.update (u);            U.setborn (Sdf.parse ("1988-12-22"));            No meaning            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 now a persistent object, remember that: if an object and is persisted state, then at this time to make various modifications to the object, or call multiple update, save method, Hibernate will not send SQL statements, Only when a thing is submitted, hibernate will compare the current object with the persisted object previously saved in the session, send an update SQL statement if it is not the same, or the UPDATE statement will not be sent

Hibernate:insert into T_user (born, password, username) VALUES (?,?,?) Hibernate:update T_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, yes, Hibernate will also emit two SQL statements, the same principle

Hibernate:insert into T_user (born, password, username) VALUES (?,?,?) Hibernate:update T_user set born=?, password=?, Username=? where id=?

5.testpersistent04

Session = Hibernateutil.opensession ();            Session.begintransaction ();            At this time U is persistent            User u = (user) session.load (User.class, 4);            Because the object is inconsistent with the object in the session, SQL completion update            u.setusername ("BBB") is issued.            Session.gettransaction (). commit ();

Let's see how many SQL statements are emitted at this point? Also remember that when the session calls the load, get method, and if there is an object in the database, the object becomes a persisted object that is hosted by the session. So, at this point, if the object is being manipulated, it will also be compared to the persisted object in the session when committing the transaction, so two SQL statements are sent here

Hibernate:select user0_.id as id0_0_, User0_.born as born0_0_, User0_.password as password0_0_, user0_.username as Userna me0_0_ from T_user user0_ where user0_.id=? Hibernate:update T_user set born=?, password=?, Username=? where id=?

6.testpersistent05

Session = Hibernateutil.opensession ();            Session.begintransaction ();            At this time U is persistent            User u = (user) session.load (User.class, 4);            U.setusername ("123");            Empty session            Session.clear ();            Session.gettransaction (). commit ();

Looking at this example, when we load the user object, the user is the persisted object, the object exists in the session cache, and then we modify the user and then call the Session.clear () method. This time will be the session cache object empty, then there is no user this object, this time when committing the transaction, it is found that there is no such object in the session, so there is no action, so this will only send a SELECT statement

Hibernate:select user0_.id as id0_0_, User0_.born as born0_0_, User0_.password as password0_0_, user0_.username as Userna me0_0_ from T_user user0_ where user0_.id=?

7.testdetached01

Session = Hibernateutil.opensession ();            Session.begintransaction ();            At this point you are an offline object that is not hosted by the session            user U = new user ();            U.setid (4);            U.setpassword ("Hahahaha");            When you execute save, you always add a piece of data, and the ID will generate            session.save (u)            according to the rules defined by hibernate. Session.gettransaction (). commit ();

We see that when U.setid (4) is called, you are an offline object, because there is a Id=4 object in the database, but the object is not hosted by the session, so this object is an offline object, to make the offline object into a persistent object, What method should be called? We know that calling the Save method can turn an object into a persisted object, but when save is executed, Hibernate inserts another data into the database based on the ID's build policy, so if you call the Save method, the database sends an inserted statement:

Hibernate:insert into T_user (born, password, username) VALUES (?,?,?)

So for offline objects, we can't use the Save method if we want to make it a persisted object, but we 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 completing the update, it will also become persistent state            session.update (u);            U.setborn (Sdf.parse ("1998-12-22"));            U.setpassword ("World");            U.setusername ("World");            A SQL            session.update (u) is emitted;            Session.gettransaction (). commit ();

At this point we see that when the Update method is called, you have become a persistent object, then if you modify the U object at this time, when the transaction commits, the object will be compared with the persisted object in the session, if different to send an SQL statement

Hibernate:update T_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 completing the update, it will also become persistent state            session.update (u);            U.setborn (Sdf.parse ("1998-12-22"));            U.setpassword ("Lisi");            U.setusername ("Lisi");            Throws an exception            U.setid (333);            Session.gettransaction (). commit ();

Let's look at this example, the previous operation, after calling the Update method, the user becomes a persistent object, after some modifications to the user, and then by the U.setid (333) method set the ID of U, then this time, hibernate will error, Because our u is currently a persisted object, if you try to modify the value of the ID of a persisted object, you will throw an exception, which should pay special attention to

Org.hibernate.HibernateException:identifier of an instance of Com.xiaoluo.bean.User is 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 point you are already instantaneous object, will not be managed by session and database            U.setpassword ("Wangwu");            Session.gettransaction (). commit ();

Then we take a look at this example, here after calling the Session.delete () method, then you will become an instantaneous object, because there is no longer the object in the database, since you are already an instantaneous object, then the u then make a variety of modifications to the operation, Hibernate does not send any modification statements, so only one DELETE statement will occur:

Hibernate:delete from T_user where id=?

11.testdetached05

Session = Hibernateutil.opensession ();            Session.begintransaction ();            User U = new user ();            U.setid (4);            U.setpassword ("Zhaoliu");            If you perform an update operation if you are offline, perform a save operation if it is instantaneous            (Note: This method is not commonly used            session.saveorupdate (u);            Session.gettransaction (). commit ();

Here we look at Saveorupdate this method, this method is actually a "lazy" method, if the object is an offline object, then after executing this method, actually called the Update method, if the object is an instantaneous object, then the Save method is called, Remember: If an object sets an ID value, such as U.setid (4), then the object is assumed to be an offline object, and an update operation is performed.

Hibernate:update T_user set born=?, password=?, Username=? where id=?

If I comment out U.setid (4) At this point, then you are an instantaneous object, then the save operation will be executed and an INSERT statement will be sent

Hibernate:insert into T_user (born, password, username) VALUES (?,?,?)

12.testdetached06

Session = Hibernateutil.opensession ();            Session.begintransaction ();            U1 is already persistent state            User u1 = (user) Session.load (user.class, 3);            System.out.println (U1.getusername ());            U2 is offline state            user U2 = new user ();            U2.setid (3);            U2.setpassword ("123456789");            At this point the U2 will become persistent state, in the session cache there are two of the same objects, in the session can not be two copies, or will throw exception            Session.saveorupdate (U2);

Let's take a look at this example, when our U1 is already a persisted object, stored in the session cache, U2 by calling the Saveorupdate method, also becomes a persistent object, which is also saved in the session cache, At this point in the session cache there is a persistent object has two reference copies, this time hibernate will be error

Org.hibernate.nonuniqueobjectexception:a different object with the same identifier value is already associated with the session: [Com.xiaoluo.bean.user#3]

A session cannot exist double copy of a persistent object, to solve this method, we will introduce another session of the method of the merge method, the role of this method is to solve a persistent object two sub-copy problem, This method merges two objects together to become an object.

Session = Hibernateutil.opensession ();            Session.begintransaction ();            U1 is already persistent state            User u1 = (user) Session.load (user.class, 3);            System.out.println (U1.getusername ());            U2 is offline state            user U2 = new user ();            U2.setid (3);            U2.setpassword ("123456789");            At this point the U2 will become persistent state, in the session cache there are two of the same objects, in the session can not be two copies, or will throw an exception//            Session.saveorupdate (U2);            The merge method determines whether the same object already exists in the session and merges two objects into the            Session.merge (U2) if one exists;            Best Practice: Merge is generally not            session.gettransaction (). commit ();

We see that by calling the merge method, the two persisted objects in the session are merged into an object, but the merge method is not recommended for use

Hibernate:select user0_.id as id0_0_, User0_.born as born0_0_, User0_.password as password0_0_, user0_.username as Userna me0_0_ from T_user user0_ where user0_.id=?zhangsanhibernate:update t_user set born=?, password=?, Username=? where id=?

Finally finished writing this essay, this essay may be less conceptual content, basically through the test case to analyze hibernate three states may appear in various situations.

Finally, summarize:

①. For an object that has just been created, if the object does not exist in the session and in the database, then the object is instantaneous (Transient)

②. The instantaneous object calls the Save method, or the offline object calls the Update method to make the object a persisted object, and if the object is persisted, any modifications to that object will be compared to it when the transaction is committed and, if different, an UPDATE statement is sent, otherwise the statement will not be sent

③. The offline object is that the object exists in the database, but the object is not hosted by the session

Original from: http://www.cnblogs.com/xiaoluo501395377/p/3380270.html if need to reprint, please specify the source, thank you!!

Hibernate three states

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.