JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Object Lifecycle, Persistence, and Session Management


Hibernate Object Lifecycle

  •  Hibernate considers objects it can  manage to always be in one of four states
                 – Transient
                 – Persistent
                 – Removed
                 – Detached

  •   Objects transition from state to state through various method calls.

Transient State

  • All objects start off in the transientstate
    – Account account = new Account();
    • account is a transient object
  •  Hibernate is not aware of the object instance.
  •  Not related to database row  
    • No value for accountId.
  • Garbage collected when no longer referenced by any other objects

Persistent State

  • Hibernate is aware of, and managing, the object Has a database id
    • Already existing object retrieved from the database
    • Formerly transient object about to be saved
  • This is the only state where objects are saved to the database
    • Modifications made in other states are NOT saved to the database while the object remains in that state
    • Changes to objects in a persistent state are automatically saved to the database without invoking session persistence methods
  • Objects are made persistent through calls against the Hibernate session
    • – session.save(account);     – session.lock(account);
      – session.update(account);  – session.merge(account);
Session session = SessionFactory.getCurrentSession();
// ‘transient’ state – Hibernate is NOT aware that it exists
Account account = new Account();
// transition to the ‘persistent’ state. Hibernate is NOW
// aware of the object and will save it to the database
session.saveOrUpdate(account);
// modification of the object will automatically be
// saved because the object is in the ‘persistent’ state
account.setBalance(500);
// commit the transaction
session.getTransaction().commit();


Removed State

  • A previously persistent object that is deleted from the database
    – session.delete(account);
  • Java instance may still exist, but it is ignored by Hibernate
    – Any changes made to the object are not saved to
    the database
    – Picked up for garbage collection once it falls out
    of scope
           • Hibernate does not null-out the in-memory  object

Session session = SessionFactory.getCurrentSession();
// retrieve account with id 1. account is returned in a ‘persistent’ state
Account account = session.get(Account.class, 1);
// transition to the ‘removed’ state. Hibernate deletes the
// database record, and no longer manages the object
session.delete(account);
// modification is ignored by Hibernate since it is in the ‘removed’ state
account.setBalance(500);
// commit the transaction
session.getTransaction().commit();
// notice the Java object is still alive, though deleted from the database.
// stays alive until developer sets to null, or goes out of scope
account.setBalance(1000);


Detached State

  • A persistent object that is still referenced after closure of the active session
    – session.close() changes object’s state from persisted to detached
  • Still represents a valid row in the database
  • No longer managed by Hibernate
    • Changes made to detached objects are not saved to the database while object remains in the detached state
    • Can be reattached, returning it to the persistent state and causing it to save its state to the database
      • update();
      •  merge();
      • lock(); // reattaches, but does not save state
Session session1 = SessionFactory.getCurrentSession();
// retrieve account with id 1. account is returned in a ‘persistent’ state
Account account = session1.get(Account.class, 1);
// transition to the ‘detached’ state. Hibernate no longer manages the object
session1.close();
// modification is ignored by Hibernate since it is in the ‘detached’
// state, but the account still represents a row in the database
account.setBalance(500);
// re-attach the object to an open session, returning it to the ‘persistent’
// state and allowing its changes to be saved to the database
Session session2 = SessionFactory.getCurrentSession();
session2.update(account);
// commit the transaction
session2.getTransaction().commit();






Saving Changes to the Database

  • Session methods do NOT save changes to the database
    • – save();
    • – update();
    • – delete();
  • These methods actually SCHEDULE changes to be made to the database
  • Hibernate collects SQL statements to be issued
  • Statements are later flushed to the database
    • Once submitted, modifications to the database are not permanent until a commit is issued
      • session.getTransaction().commit();
The Persistence Context

  • Managed object environment
    • No API or concrete object to reference, just conceptual
    • Thought of as containing:
      • Graph of managed persistent instances
      • List of SQL statements to send to the database
  • Each Hibernate session is said to have one ‘persistence context’

=\



Hibernate Session

  • Single threaded non–shared object that represents a unit of work
  •  Used to retrieve objects from the database
  •  Contains the ‘persistence context’
  •  Used to schedule changes to be made in the database.
\
Session API –Object Persistence

  • session.save(Object o)
    •  Schedules insert statements to create the new object in the database
  •  session.update(Object o)
    •  Schedules update statements to modify the existing object in the database
  • session.saveOrUpdate(Object o)
    •  Convenience method to determine if a ‘save’ or ‘update’ is required
  • session.merge(Object o)
    •  Retrieves a fresh version of the object from the database and based on that, as well as modifications made to the object being passed in, schedules update statements to modify the existing object in the database.
  • session.get(Object.class, Identifier)
    •  Retrieves an object instance, or null if not found
  • session.lock(Object, LockMode)
    •  Reattaches a detached object to a session without scheduling an update
    •  Also used to ‘lock’ records in the database
  • session.delete(Object)
    •  Schedule an object for removal from the database
  • session.clear()
    • Removes all objects from persistence context, changing all their states from persistent to detached