Hibernate advanced Hibernate three states for persistent objects

Source: Internet
Author: User

Hibernate persists objects in three states: Instantaneous objects (Transient Objects), persisted objects (Persist Objects), offline objects (Detached Objects).


three different states: Instantaneous object (Transient Objects): Objects initialized with the new operator are not immediately persistent. Their state is instantaneous, meaning that they do not have any behavior associated with the database table, as long as the application is not referencing these objects (no longer referenced by any other object), their state will be lost and reclaimed by the garbage collection mechanism.
Persisted object (Persist Objects): A persistent instance is any instance that has a database identity. It has persistent manager session Unified management, persistent instances are operations in the transaction-their state is synchronized with the database at the end of the transaction. When a transaction commits, the in-memory state is synchronized to the database by executing SQL insert,update and DELETE statements.

Offline object (Detached Objects): After the session is closed, the persisted object becomes an offline object. Offline means that the object can no longer be synchronized with the database, and they are no longer managed by hibernate.


State transitions:

The three states of a persisted object can be converted to each other, and the specific conversion process is:






Example Explanation: To→po:

<strong><span style= "FONT-SIZE:18PX;" ><strong><span style= "FONT-SIZE:18PX;" >public void TestSave1 () {session session = NULL; Transaction tx = Null;try {session = Hibernateutils.getsession (); tx = Session.begintransaction ();//transient Status User User                                                       = new User (), User.setname ("Zhangsan"), User.setpassword ("123");D ate date=new Date ();                            SimpleDateFormat temp=new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");                            String Date2=temp.format (date);                                                         Date Date3=temp.parse (DATE2);                          User.setcreatetime (Date3); User.setexpiretime (Date3); Persistent the state of the//persistent state object, when the object's properties change//hibernate when the cache (dirty data check), and the database synchronization session.save (user); user.setname ("Lisi"); Tx.commit ();} catch (Exception e) {e.printstacktrace (); if (tx! = null) {Tx.rollback ();}} finally {hibernateutils.closesession (session);} Detached status}&LT;/SPAN&GT;</strong></span></strong> 

results of the output:

<strong><span style= "FONT-SIZE:18PX;" ><strong><span style= "FONT-SIZE:18PX;" >hibernate:insert into User (name, password, createtime, expiretime, id) VALUES (?,?,?,?,?) Hibernate:update User set name=?, password=?, createtime=?, Expiretime=? where id=?</span></strong></span></strong>

To→po→do→po:

<strong><span style= "FONT-SIZE:18PX;" ><strong><span style= "FONT-SIZE:18PX;" >public void TestSave3 () {session session = NULL; Transaction tx = NULL; User User = Null;try {session = Hibernateutils.getsession (); tx = Session.begintransaction ();//transient state user = new User (                                           ); User.setname ("Zhang San"); User.setpassword ("123");D ate date=new Date ();                 SimpleDateFormat temp=new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");                 String Date2=temp.format (date);                                              Date Date3=temp.parse (DATE2);              User.setcreatetime (Date3); User.setexpiretime (Date3); Persistent the state of the//persistent state object, when the object's properties change//hibernate when the cache (dirty data check), and the database synchronization session.save (user); user.setname ("Lisi"); Tx.commit ();} catch (Exception e) {e.printstacktrace (); if (tx! = null) {Tx.rollback ();}} finally {hibernateutils.closesession (session);} Detached status User.setname ("Wangwu"); try {session = Hibernateutils.getsession(); Session.begintransaction ();//Detached The object of the State into session management//object//persistent state that will become persistent state. The cleanup cache is synchronized with the database session.update (user); Session.gettransaction (). commit ();} catch (Exception e) {e.printstacktrace (); Session.gettransaction (). rollback (); finally {hibernateutils.closesession (session);}} </span></strong></span></strong>


results of the output:

<strong><span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><strong>hibernate:insert into User (name, password, createtime, expiretime, id) VALUES (?,?,?,?,?) Hibernate:update User set name=?, password=?, createtime=?, Expiretime=? where id=? Hibernate:update User set name=?, password=?, createtime=?, Expiretime=? where id=?</strong></span></span></strong>



Summary: these three states are mainly to see if they exist in the database and session:

Transient
* Not managed by the session
* There are no records in the database that match it
Persist
* Included in Session management
* There are records in the database that match it
* When the property is changed, when the cache is cleaned (dirty data check) is automatically synchronized with the database
Detached
* Not managed by the session
* There are records in the database that match it

Hibernate advanced Hibernate three states for persistent objects

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.