Hibernate persistence life cycle--three states

Source: Internet
Author: User

through the introduction of the previous blogs, we found that hibernate's implementation of persisting the business entity to the database works by "Mapping xml". So what's the origin of this XML?
mapping files (HBM) The premise of a reasonable existence is the existence of a corresponding entity-the business entity.

Today this blog is about the process of entity-to-database, which is a persistence process from code to data, which is the life cycle of hibernate to achieve ORM persistence.

I. Persistent object life cycle

is a state diagram of hibernate persistence, it can be seen that the ORM process has undergone three states: Transient,persistent,detached.


First, understand the three states of the Entity object: Transient object, Persist object,detached object contains information:
1. Transient object (Temporary objects): It is clear that in the figure, at the beginning position: We have new object, entered the Transient state. That is, objects initialized with the new operator are not immediately persisted, their states are instantaneous, they do not have any behavior related to database tables, and as long as the program is no longer invoking those objects, their state is lost and reclaimed by the garbage collection mechanism.

code example:

         <span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >//creating a Transient object          user user = new user ();          User.setname (userName);          User.setpassword ("test");          User is still a transient object </span>

2, Persist object (persisted): The persistent instance is any instance that has a database identity. It is managed uniformly by the persistence Manager session, the persistent instance is the operation in the transaction ———— their state is synchronized with the database at the end of the transaction, and when the transaction commits, the state of the memory is synchronized to the database by executing the insert,update of SQL and the DELETE statement.
code example:

                       Create a transient object user user = new user (); user.setname (userName); User.setpassword ("test");//user is still a transient object session session = Sessionfactory.opensession (); Tansaction tx = Session.begintansaction ();//At this time the user is still a transient object session.save (user);//At this time the user has become a persistent state

3, Detached object (Offline objects): After the session is closed, the persisted object becomes the offline object, the offline means that the object can no longer be kept in sync with the database and is no longer managed by hibernate.
code example:

           <span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >//creating a Transient object            user user = new user ();            User.setname (userName);            User.setpassword ("test");            User is still a transient object                        session session = Sessionfactory.opensession ();            Tansaction tx = Session.begintansaction ();            At this point the user is still a transient object            session.save (user);            At this point the user has become persistent state            tx.commit ();            Session.close ();     Clear ()   evict ()  3 variable free states the            user object has become a Free State </span>

Note: in the ORM process, the flag that distinguishes the three states is "whether the session" is administered.
Transient, detached state are not managed by the session, only persistent by the session management, note that the session here is hibernate session, not the session of the user in HTTP. The entity object is in the state managed by the Hibernate framework, which means that the entity object is related to the instance of the session object, and any changes that are made to the entity are persisted to the database layer by hibernate.

Use this table to illustrate the differences between the three states:


Two, the reciprocal conversion between hibernate three states


Or this picture, we look at the arrow in the diagram, the three states can be converted between each other.

Figure,There are two types of transitions between three states

1.Enter Session, the blue Arrow, is managed by the session.
Three ports: transient, before detached,transient.
How to: Modify: Save, Saveorupdate, lock, so whether in transient, or detached can be converted to persistent state.
Query: Get,load,find,iterator ().


2.out Session, Red Arrows are not managed by the session.
One mouth: persistent.
Method: Evict,close,clear,delete.

Third, persistent special instructions


1, direct call to persistent.

In the above description, there is a special case: query: Get,load,find,iterator (). So hibernate's three-state life cycle does not include the query nature of the operation, the diagram can obviously see the query statement from the program can be directly called to persistent. Queries are only related to persistent.
From an instance, describe:

Session session = Sessionfactory.opensession (); Tansaction tx = Session.begintansaction (); User user = (user) session.load (user.class, "key"),------equivalent to HQL read from DB is of course the persistent state//user object is already persistent too object tx.commit (); Session.close ();

2, Automatic Update in persistent

1th said is the direct transfer to persistent, here said is through the transitent, into the persistent after the automatic update. Look at the following code:

Create a Transient object  user user = new user ();  User.setname (userName);  User.setpassword ("test");  User is still a transient object session session = Sessionfactory.opensession (); Tansaction tx = Session.begintansaction ();//At this time the user is still a transient object Session.save (user),//At this time the user has become persistent state tx.commit ();// Persistent Automatic Update code tansaction TX2 = Session.begintansaction (); User.setpassword ("PDW"); Tx2.commit ();// Although the session's save () method is not used to save the user object in this thing, USR is persistent too,//so any modifications made to the user object persist to the database   ---------Persistent State Auto-update// Then the password in the database also becomes the PDW               session.close ();

When user enters the persistent, we can directly set the operation, because it is in the persistent, so we do not have to go through "save" or "saveorupdate" to enter persistent again. This is like, we bought the amusement park pass to play, as long as go in, you can play each project, do not have to play a project to go out to buy tickets.


This is the process of persisting hibernate objects to a database, three states, One life cycle, the transition between States, they all follow certain "thing development law".

This blog is just a rough introduction to the basics and is part of the user manual that we develop that must be understood. Of course, there are APIs, detailed records of Hibernate included in the method, features, here is not introduced, these are just getting started, get started, or need a lot of practice.

Hibernate persistence life cycle--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.