Hibernate object three states

Source: Internet
Author: User

There are three types of objects in Hibernate:

1,transient instantaneous: Object just new out, not set the ID, set the other value.

2,persistent persistent: called Save (), Saveorupdate (), becomes persistent, has ID

3,detached: When the session close () is finished, it becomes Detached.

Example program:

Teacher Class:

1  Packagecom.oracle.hibernate.id;2 3 Importjavax.persistence.Entity;4 ImportJavax.persistence.GeneratedValue;5 Importjavax.persistence.Id;6 7 @Entity8  Public classTeacher {9 Ten      One      A     Private intID; -     PrivateString name; -      the     PrivateString title; -  - @Id - @GeneratedValue +      Public intgetId () { -         returnID; +     } A  at      Public voidSetId (intID) { -          This. ID =ID; -     } -  -      PublicString GetName () { -         returnname; in     } -  to      Public voidsetName (String name) { +          This. Name =name; -     } the  *      PublicString GetTitle () { $         returntitle;Panax Notoginseng     } -  the      Public voidSettitle (String title) { +          This. title =title; A     } the      +      -}
View Code

Test class:

 Packagecom.oracle.hibernate.id;Importorg.hibernate.HibernateException;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.AnnotationConfiguration;Importorg.hibernate.cfg.Configuration;ImportOrg.junit.AfterClass;ImportOrg.junit.BeforeClass;Importorg.junit.Test; Public classHibernatecoreapitest {Private StaticSessionfactory SF =NULL; @BeforeClass Public Static voidBeforeclass () {//Try-chatch is to resolve a bug that does not prompt for JUnit errors        Try{SF=Newannotationconfiguration (). Configure (). Buildsessionfactory (); } Catch(hibernateexception e) {e.printstacktrace (); }} @Test Public voidTestteacher () {Teacher T=NewTeacher (); //No ID setT.setname ("Li"); T.settitle ("High"); //execution to this point, the state is transient. //there is a session object in memory,Session session =sf.getcurrentsession ();        Session.begintransaction (); //save to become persistentSession.save (t); //Print out IDSystem.out.println (T.getid ()); //automatically close session after commit, becomes detached statesession.gettransaction (). commit (); } @AfterClass Public Static voidAfterclass () {sf.close (); }}
View Code

Detailed code interpretation, memory analysis:

1, when theacher t = new Teacher (); After t.setxxx, the T at this time has no ID, just has a teacher object T in memory. At this point the state is transient.

2, when the session session is executed = Sf.getcurrentsession (); There will be a session object in memory, a HashMap in the session object, he is actually the cache: an area in memory, There is a series of objects that you want to improve the reading efficiency (references). This HashMap key, which is used to hold the id,value of the object to be persisted, holds our object, where T is, in fact, a reference to the object being saved, pointing to the T object. This is all about preparation, no call to the Save method, nothing inside. When the Session.save (t) is executed, there is a T object in the session's cache, and the key has an ID 1,value pointing to the T object. When the Save method is called, an INSERT statement is issued, and there is a T object in the data.

So after save, there is a T object in memory, and a T object in the database, called the persistent state.

The benefits of doing this are:

Improve efficiency, if you want to find id=1 objects, directly from the memory to check, rather than go to the database to find.

3, when executed session.gettransaction (). commit (); session automatically closed, the session object is not, the cache is gone. Look at the figure, this time the T object in memory, but HashMap point to T. The T object has been removed from the session's management, known as the detached de-tube state.

Summarize:

The key to the three states of Hibernate objects is:

A) there is no ID

b) ID in the database there is no

c) There is no in memory (session cache)

Three different states:

A) Transient: an object in memory, no ID, no cache

b) Persistent: There are objects in memory, in the cache, in the database (ID)

c) Detachd: Memory has object, cache not, database has

Hibernate object 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.