Hibernate Persistence object status, hibernate object

Source: Internet
Author: User
Tags sesion

Hibernate Persistence object status, hibernate object

In Hibernate, persistence objects are divided into three phases during the operation. These three phases are related to session cycles.

They are Transient, persistent, and Detached)

Instantaneous status

Use the new command to open up memory java objects. for example: Student stu = newStudent (); if there is no variable to reference it, it will be reclaimed by jvm. an instantaneous object exists in memory in an isolated way. It only carries information and is not associated with the data in the database. Through the save () and saveOrUpdate () Methods of the session, you can associate an instantaneous object with the database, and insert the information carried by the instantaneous object into the database through the ing made in the configuration file, this instantaneous object becomes a persistent object with the same id as the database record (Hibernate automatically assigns the id to it)

Instantaneous features:

(1) not associated with the Session instance

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

Persistent state

Persistent instances have corresponding records in the database. is associated with Session and Transction. in sessin, Persistent object changes do not immediately change data. the database must be synchronized only after the Transcation is terminated and commit. the persistent object before this is a dirty object.

Data queried using methods such as find, get, load, and iterater are persistent objects. If an instantaneous object is referenced by a persistent object, the object will also become a persistent object.

If the delete () method is used, it becomes a transient object. When a Session executes close (), clear (), evict (), the persistent object becomes a hosted object.

Persistent features:

(1) associate with the Session instance

(2) records associated with persistent objects in the database

Code example: public void testSave () {Session session = null; Transaction tx = null; try {session = HibernateUtils. getSesion (); tx = session. beginTransaction (); // transient Instantaneous State User user = new User (); user. setName ("sdf"); user. setPassword ("hanhan23"); user. setCreateTime (new Date (); // persistent, session reference, po persistent session. save (user); // is referenced and becomes persistent // but is a dirty Object user. setName ("lisd"); tx. commit (); //} catch (Exception e) {e. printStackTrace (); tx. rollback ();} finally {HibernateUtils. closeSession (session);} // After the sesion is closed in the deteached status}

The preceding example shows how to convert the instantaneous state to a persistent State object.

Managed status

After the Session associated with the persistent object is closed, the object becomes hosted. The reference to the managed object is still valid, and the object can be modified. If the managed object is re-associated to a new Session, it will be converted to persistent again. Changes made during hosting will be made persistent to the database.

The managed status has the database id, so it can be re-associated with the persistent layer by means of update (), saveOrUpdate (), and lock.

Hosting features:

(1) essentially the same as the instantaneous object

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

Summary:

Through the comparison of the above three types, it is realized that the object in Hibernate is a persistent data update in session management. the transition between States is closely related to the object lifecycle. the created memory is a free state. If it is saved as a persistent State (or referenced by the session) by the session ). once sesion is disabled or delete becomes managed. please give more comments at the beginning.


How does one understand the persistence problem in hibernate?

The so-called persistence is from the business model perspective. According to the object-oriented analysis and design method, we first draw an object model based on the business shape, then write code, and then call each other between objects, and the system will run. Object-oriented is awesome, but there is a huge problem that all objects are in the computer's memory. Once the machine is turned off, everything disappears. So we need to have this function: Save the objects in the memory to the database, and take them out from the database when necessary.

The simplest way is to write an SQL statement in the object and insert/update the object attribute. When necessary, select data from the database and set it to the object attribute. This is the best way. In this way, the object will be persistent.

A more elegant way is to define a table, in which a ing between object attributes and database fields is recorded. When we need an object, based on this table, the object attribute value is automatically assigned, and the modified attribute can be automatically updated back to the database. With this function, the user of the object feels like there is no database, as if these objects are originally placed in the memory. This is the orm.

Persistence is to put the cached objects in the database to make them persistent. For objects that require persistence, its lifecycle is divided into three states: temporary state, persistent state, and free state.
Temporary State: a java object in the temporary state is called a temporary object. It has just been created with the new statement and has not been persisted and is not in the session cache.
Persistence: the object has been persisted and added to the session cache. A persistent java object is called a persistent object.
Free State: a java object that has been persisted but is not in the session cache.
Persistence object features:
Stored in the cache of a session case. It can be said that the persistent object is always associated with a session case.
. The persistence object corresponds to the relevant records in the database
When the Session clears the cache, it synchronously updates the database based on the attribute changes of the Persistent Object.
The save () method of the Session changes the temporary state to the persistent state.
The update (), saveOrUpdate (), and lock () Methods of the Session change the free state to the persistent state.

What are the three persistence states of hibernate objects?

Instantaneous State: Simply put, you create an object in the program and it is not associated with the session.
Persistent state: the object is associated with the session, and the object is under hibernate framework management.
Free State: In the persistent state, the object and session are lost, such as session. close () or session. flush ()
But the data has been stored in the database.

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.