Analysis of three states of objects in ORM advanced Hibernate

Source: Internet
Author: User


orm Advanced ORM Simple Introduction

ORM advanced Hibernate Simple 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 the three states of an object. Transient state (transient), persistent state (persistent). Free State (detached). Let's explain the three states separately.

Temporary status (Transient Objects)

through New the object created is a temporary object, assuming that the temporary object does not do whatever it is, it will be recycled by GC, stating that the object of such State does not have the ability to interact with the database and exists outside the session cache.

the temporary object is not in session in the cache, it is not associated with any session instance. And there is no corresponding record in the database, and the code can set the OID value for the temporary object. But finally hibernate does not use, it uses the built-in primary key generation strategy.

in the following scenario, the Java object enters a temporary state

(1) when a Java object has just been created with the new statement. It is in a temporary state and does not correspond to any records in the database at this time.

(2) The Delete () method of the Session can turn a persisted object or a free object into a temporary object. For a temporary object, the Delete () method deletes its corresponding record from the database, and for the persisted object, the Delete () method deletes the corresponding record 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. Unified management by the session . Persistent instances are operations in a transaction, and their state is synchronized with the database at the same time that the transaction is over.

When a transaction is committed. The in-memory state is synchronized to the database by running SQL Inset,update and DELETE statements.

Summary of Persistence Features:

1. in the cache of the session instance. It is also possible to say that persistent objects are associated with a session instance

2. Persistent objects The records in the fish database correspond

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

4.Hibernate guaranteed to be in the same session The cache for the instance, each record in the database table is only the corresponding persisted object. That is, hibernate assigns the persistent object a unique OID to identify the persisted object

in the following scenario, the 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); *//* Another situation user user= (user) Session.load (user.class, 1001); *//* * Above the  user is persisted. In the cache. It is also possible to interact with the database *///commit transactions. 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. The persisted object becomes a free object, at which point the object can no longer interact with the database, that is, it cannot be synchronized. No longer managed by Hibernate .


Summary of characteristics of free objects:  

1. do not cache at session level. But there is the OID value.

2. There are also relative records in the database (unless the record is deleted by other operations). But no longer has the ability to interact with the database and can no longer synchronize.

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 are able to 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 attribute changes cannot be synchronized with the database, and the free object is transformed by a persisted object, there is a corresponding record in the database (unless deleted), and the temporary object is not recorded in the database, in the appropriate analogy. The free object is equivalent to the retired elderly. Retired people are not in the post. But the database also has a record of his past, 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 advantages for our very good processing objects, so we need to figure out the three objects. And the transitions between objects!

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.