Recent Posts

Sunday, 22 May 2016

Persistent Object Life Cycle

     
     Persistent Object has three life cycle states
1. Transient state
2. Persistent state
3. Detached state
Persistent Object Life Cycle

Note
* Object is associated with session means object reference is available to the session object
* Object present in the database means object identifier value is available in database primary key column (Non identifier column values are not matter)
Transient state
     An object is said to be in transient state, when it is not associated with session and not present in data base.
Example: Table
Application code
Account account = new Account();
account.setAccountld(1003);
account.setName("Dillesh");
account.set8alance(l500);
     In the above example account object is not associated with session and there is no matching record in the ACCOUNT table. So we can say that account object is in transient state. In this state object is non-transactional. i.e., object is not synchronized with record. i.e., Modifications done to entity, doesn't save into database.
Persistent state
     An object is said to be in persistence state, when it is associated with session as well as object present in database.
Example: Table
Application code
Account account = (Account) session.get(Account.class,1002);
* In the above example account object is associated with session and there is a matching record in ACCOUNT table. So we can say that account object is in persistent state.
* In this state object is transactional. 1.e. the object is synchronized with database record.
* Changes made to objects in a persistent state are automatically saved to the database without invoking session persistence methods
Detached state
     An object is said to be in detached state, when the object is not associated with session but present in data base.
Example: Table
Application code 
Account account = new Account();
account.setAccountld(1004);
account.setName("cherry");
account.setBalance(2100);
//Now the account object is said to be in transient state
session.save(account);
// Now the account object is said to be in persistence state
session.close();
// Now the account object is said to be in detached state.
* In the above example after callingsession.close() method, account object is moved to Detached state from persistent state. As session i s garbage collected, if we try to perform some modifications to entity object those changes will not be stored into database.
Saving Changes to the Database
* Session methods do NOT save changes to the database
1. save();
2. update();
3. delete();
* These methods actually SCHEDULE changes to be made to the database
* Once Transaction committed, all the queries will be pushed to the database
session.getTransaction().commit();

Next Tutorial   Hibernate Session Methods

Previous Tutorial  Hibernate Configuration and Sessions

No comments:

Post a Comment