Three States of java objects in Hibernate: hibernatejava

Source: Internet
Author: User

Three States of java objects in Hibernate: hibernatejava

  • Transient status (Transient)

After an object is created through new, the object is not immediately persistent, and it is not associated with the data in the database. At this time, the state of the Java object is instantaneous.

Session knows nothing about Java objects in the instantaneous state. when an object is no longer referenced by other objects, all its data will be lost, and the object will be processed by the Java Virtual Machine According to the garbage collection mechanism.

  • Persistent)

When an object is associated with a Session and managed by the Session, it is in the persistent state. Objects in the persistent State have database IDS (primary key values in the database ).

When is the object associated with the Session? There are two methods:

First, when the Sesison query interface, the get () method, or the load () method is used to load objects from the database, the loaded object is associated with a record in the database table. In this case, the object is associated with the Session where it is loaded;

Second, when an object in the instantaneous state is connected to the Session through the save () method or SaveOrUpdate () method of the Session, the Java object is also associated with the Session.

For objects in the persistent state, the Session will continuously track and manage them. If the internal state of the object changes, Hibernate will choose the appropriate time (such as when the transaction is committed) fix the changes to the database.

  • Free Status

An object in the persistent state is in the Free State after it is detached from the management of its associated nSession.

When an object is in the Free State, the Session cannot guarantee that the data contained in the object is consistent with the records in the database, because Hibernate cannot perceive any operations on the object.

The Session provides two methods (update () and merge () to associate an object in the Free State with a new Session.

In this case, the object state is changed from the Free State to the persistent state.

2. transition between three states:

Use the new Keyword to construct an object. The object state is instantaneous.

1. Transition from instantaneous to persistent

After you save an object by using the save () or saveOrUpdate () method of the Session object, the state of the object is changed from instantaneous to persistent.

Use the get () or load () method of the Session object to obtain the object. The object state is persistent.

2. Transition from permanent State to instantaneous state

After the delete () method of the Session object is executed, the object changes from the original persistent state to the Instantaneous State, because the object is not associated with any database data.

3. The persistent state changes to the Free State.

The evict (), clear (), or close () Methods of the Session object are executed, and the object changes from the original persistent state to the Free State.

4. Transition from free to persistent

Re-obtain the Session object and execute the update () or saveOrUpdate () method of the Session object. The object changes from the Free State to the persistent state, and the object is associated with the Session object again.

5. Transition from free to instantaneous

Execute the delete () method of the Session object, and convert the object from the Free State to the instantaneous state.

When an object in the instantaneous or free state is no longer referenced by another object, it will be processed by the Java Virtual Machine According to the garbage collection mechanism.

3. Difference Between get () and load () in Hibernate

When the get () method of the Session is used, if the loaded data does not exist, the get () method returns a NULL value. However, the load () method is used. If the loaded data does not exist, an exception is thrown.

I. load Loading Method

When you use the load method to obtain an object, hibernate uses the delayed loading mechanism to load the object, that is, when we use session. when the load () method is used to load an object, no SQL statement is issued. The current object is actually a proxy object. This proxy object only saves the id value of the object, only when we want to use this object to obtain other attributes will we issue an SQL statement to query our objects from the database.

 Session session = HibernateUtil.currentSession();           Transaction tx=session.beginTransaction();           Dept dept = (Dept)session.load(Dept.class,1);           System.out.println(dept);
When an object is loaded using load, the delayed loading mechanism is used. The User object is actually a proxy object, and only the id attribute is contained in the proxy object.

Ii. get Loading Method

Compared with the load delay loading method, get is much more direct, when we use session. when the get () method is used to obtain an object, no matter whether this object is used, an SQL statement is issued to query the object from the database:

Session session = HibernateUtil. currentSession (); Transaction tx = session. beginTransaction (); // when the get method is used to load an object, no matter this object is not used, an SQL statement is issued, // query the Dept dept = (Dept) session from the database. get (Dept. class, 1); System. out. println (dept );

Therefore, we can see that the load loading method is better than the get loading method, because during load loading, all we get is a proxy object, when you really need to use this object, you can query it from 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.