Status of java objects on the persistence layer of hibernate

Source: Internet
Author: User

From the perspective of the persistence layer, a java object can be in one of the following four states in its lifecycle:

Temporary status (transient): It was just created with the new statement and has not been persisted and is not in the Session cache.

Persistent: it has been persisted and added to the Session cache.

Removed: it is no longer in the Session cache. The Session has been scheduled to be deleted from the database.

Detached: it has been persisted but is no longer in the Session cache.

The code is clearer:

Code

Tx = session. beginTransaction (); start the temporary state of the lifecycle Student s1 = new Student ("tom", new HashSet (); session. save (s1); Long id = s1.getId () in the life cycle transition to persistent State; s1 = null in the life cycle; Student s2 = (Student) session. get (Student. class, id); tx. commit (); session. close (); the System is in the free status during the life cycle. out. println (s2.getName (); in the Free State of Life Cycle s2 = null; End of Life Cycle


The difference between a free object and a deleted object is that the free object and the Session are completely separated. The Session plans to delete the deleted object from the database. When the Session clears the h cache, the corresponding SQL delete statement is executed to delete the corresponding records 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.