Hibernate Life cycle

Hibernate defines and supports the following objects states:
  • Transient- an object is transient if t has been instantiated using the new operator, and it is not associated with Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore. Use the Hibernate Session to make an object persistent.

  • Persistent-a persistence instance has a representation in the database and an identifier vlaue. It might have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers do not execute manual UPDATE statements, or DELETE statements when an object should be made transient.

  • Detached-a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, ofcourse, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modification) persistent again. This fetaure enables a programming model for long running units of work that require user think-time. We call them application transaction, i.e., a unit of work from the point of view of the user.

Hibernate Life cycle

Life Cycle Operations

  • The Hibernate framework is used to mange the persistence of objects. It allows objects to be associated with data present in the tables.
  • It saves/modifies and inserts records in the database tables based on values in these objects and the operations performed on them using the Hibernate interfaces.
  • Saving Objects
    • Creating an instance of a class you map with Hibernate mapping does not automatically persist the object to the database until you save the object with a valid Hibernate session.
    • An object remains to be in "transient" state until it is saved and moved into "persistent" state.
    • The class of the object that is being saved must have a mapping file (myclass.hbm.xml).
    • Mliethods to save objects provided by Session interface.
      • public Serializable save( Object object)
      • public void save (String entityName, Object object, Serializable id)
  • Loading Objects
    • Used for loading objects from the database
    • load(..) method requires object's primary key as an identifier
    • Each load(..) method also requires which domain class or entity name to use to find the object with the id
    • The returned object, which is returned as Object type, needs to be type-casted to the domain class
      • public Object load(Class theClass , Serializable id)
  • load() v/s get()
    • Only use the load() method if you are sure that the object exists
    • load() method will throw an exception if the unique id is not found in the database
    • If you are not sure that the object exists, then use one of the get() methods
    • get() method will return null if the unique id is not found in the database

    • public Object get (Class theClass, Serializable id)
      Person person = (Person) session.get(Person.class, id);
      if (person == null){
      	    logger.info("Person is not found for id "+ id);
      }
  • Updating Objects
    • Used for updating object's state
    • Methods to save objects provided by Session interface
      • public void update( Object object)
      • public void saveOrUpdate( Object object) - This method will update the record if it exists in the database. Otherwise will insert a new record.
  • Deleting Objects
    • Removes an Object from the Database.
      • public void delete( Object object)

Object-relational impedance mismatch

Mismatch Description
Granularity Sometimes you will have an object model which has more classes than the number of corresponding tables in the database.
Inheritance RDBMSs do not define anything similar to Inheritance which is a natural paradigm in object-oriented programming languages.
Identity A RDBMS defines exactly one notion of 'sameness': the primary key. Java, however, defines both object identity (a==b) and object equality (a.equals(b)).
Associations Object-oriented languages represent associations using object references where as am RDBMS represents an association as a foreign key column.
Navigation The ways you access objects in Java and in a RDBMS are fundamentally different.