Analysis of three states of objects in ORM advanced Hibernate

Source: Internet
Author: User


Introduction to ORM Advanced ORM

ORM advanced Hibernate Introduction and Framing

The three main objects of the ORM advanced Hibernate

Analysis of three states of objects in ORM advanced Hibernate


There are three states for each object in the Hibernatea, and in these three states, hibernate will treat them differently. Let's look at the three states and the transitions between them in a picture below!


You can see that the object may have these three states, the temporary state (transient), the Persistence State (persistent), the Free State (detached). Let's explain the three states separately.

Temporary status (Transient Objects)

through New the object created is a temporary object, and if the temporary object does nothing, it is recycled by GC, stating that the object of this state does not have the ability to interact with the database and exists outside the session cache.

the temporary object is not in session cache, not associated with any session instances, and there are no corresponding records in the database, and the code can set the OID value for temporary objects, but eventually hibernate will not take, it is using the built-in primary key generation strategy.

The following scenarios, Java object enters a temporary state

(1) when a Java object has just been created through the new statement, it is in a temporary state and is not corresponding to any records in the database.

(2) The Delete () method of the Session can turn a persisted object or a free object into a temporary object. For temporary objects, the Delete () method deletes records from the database that correspond to it, and for persisted objects, the Delete () method deletes the records corresponding to it from the database and removes it from the session's cache.

Take a look at the code show

<span style= "FONT-SIZE:18PX;" >public class UserService {public      void  Add () {             //Create a   configuration          configuration cfg = Newconfiguration (). Configure ();          Sessionfactory factory = Cfg.buildsessionfactory ();         Open session Session Session         = Factory.opensession ();          Open transaction         Transaction tran = Session.begintransaction ();          Create a User object//       User user = new user ();/////       User.setid (1001);//       User.setusername ("Zhanghongjie");//       user.setpasswords ("Zhanghongjie");         /          * * The user is in a temporary state at this time. is not in the cache and does not interact with the database         */         user user= (user) Session.load (user.class, 1001);         Session.delete (user);         /          * * The user is in a temporary state at this time. is not in the cache and does not interact with the database, but there is a corresponding record in the database          *          ///COMMIT TRANSACTION. Save to Database         Tran.commit ();          Session.close ();      }} </span>

persisted objects (Persist Objects)

A persisted object is an instance that has a database identity, by the session Unified management, persistent instances are operations within a transaction, and their state is synchronized with the database while the transaction is over. When a transaction commits, the in-memory state is synchronized to the database by executing SQL inset,update and DELETE statements.

Summary of Persistence Features:

1. in the cache of the session instance, it can also be said that the persisted object is associated with a session instance

2. the records in the persistent object fish database should correspond to

3.Session When you clean up the cache (Session.flush ), the database is updated synchronously based on the properties of the persisted object

4.Hibernate guaranteed to be in the same session instance, each record in the database table is only persisted to the Chong, that is, hibernate assigns the persisted object a unique OID to identify the persisted object

The following scenarios, Java object enters persistent state

1. convert A temporary object to a persistent object through the Save () method

2. Use the get () or Load () method to turn records in a database table into persisted objects

3. objects in the list collection returned by the find () method are also persisted objects

4. Convert a free object to a persisted object by using the update () or saveorupdate () and Lock () methods

<span style= "FONT-SIZE:18PX;" >public class UserService {public void  Add () {//Create a   configuration  configuration cfg = new configuration (). Configure ();  Create Sessionfactory         Sessionfactory factory = Cfg.buildsessionfactory ();//Open sessionsession session = Factory.opensession ();  Open transaction Transaction Tran = Session.begintransaction ();  Create a User Object */* First case User user = new user ();  User.setid (1001); User.setusername ("Zhanghongjie"); User.setpasswords ("Zhanghongjie"); Session.save (user); User.setusername ("Hongjie"); Session.update (user); *//* The second case is the user user= (user) Session.load (user.class, 1001); *//* * The user of the case above  is persisted. In the cache, you can also interact with the database *///commit the transaction. Save to Database Tran.commit ();      Session.close ();   }} </span>


Free Objects (Detached Objects)

Session after closing, or clearing the session caches and clears the specified cache, and the persisted object becomes a free object, at which point the object can no longer interact with the database, i.e. it cannot be synchronized and is no longer managed by Hibernate .

Summary of characteristics of free objects:  

1. not at session level cache, but with OID values.

2. There are also relative records in the database (unless the record is deleted by other operations), but no longer have the ability to interact with the database and cannot be synchronized.

public class UserService {public void  Add () {//Create a   configuration  configuration cfg = new configuration (). Configure ();  Create Sessionfactory         Sessionfactory factory = Cfg.buildsessionfactory ();//Open sessionsession session = Factory.opensession ();  Open transaction Transaction Tran = Session.begintransaction ();  Create a User object user user = (user) session.load (user.class, 1001); Tran.commit ();      Session.close ();     /* * This time the session is closed, we can manipulate the user object, but cannot interact with the database!     */}}

the difference between a free object and a temporary object

Neither of them is a session . Association, even if the property changes can not be synchronized with the database, and the free object is changed by the persistent object, so there are corresponding records in the database (unless deleted), and the temporary object in the database is not recorded, in the proper analogy, the free object is equivalent to the retired elderly, retired elderly people are not in the post, But the database also has his previous records, while the temporary target is not formally entered the company's young people, not yet, the database also has no record of young people. The understanding of the three states of the object in hibernate has many benefits for our good handling of objects, so we need to figure out the three objects and the transitions between objects!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analysis of three states of objects in ORM advanced Hibernate

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.