` Hibernate Architecture | Java2Study.com

Hibernate Architecture

  • Hibernate is an Object Relational Mapping Tool and it acts as a Middle Layer Between Application and Database.The Followig Diagram shows complete Hibernate Architecture.

Configuraton(org.hibernate.cfg.Configuration)

  • Configuration object is created once during application intialization.Configuration Object is used to load hibernate configuration files(hibernate.cfg.xml) and hibernate Mapping files(hibernate.hbm.xml) like following.
  •  Configuration configuration = new Configuration.Configure(hibernate.cfg.xml);
    
  • Configuration Object is used to create SessionFactory Object.

SessionFactory (org.hibernate.SessionFactory)

  • SessionFactory Object is Thread Safe Object and immutable Object.
  • SessionFactory Object is created per One Per database.
  • We can Create SessionFactory Object From Configuration Object like Following.
  • Configuration configuration = new Configuration.Configure(hibernate.cfg.xml);
    SessionFactory sessionFactory = configuration.buildSessionFactory();
    
  • SessionFactory Object is an High Level Object.
  • SessionFactory is a factory of Sessions. And all Sessions of application will use the Single SessionFactory Object.
  • Multiple SessionFactory Objects will be created while using Multiple databases.
  • SesionFactory Object Holds Second level cache.

Session (org.hibernate.Session)

  • we can create Session Object from SessionFactory Object Like Following.
  • Configuration configuration = new Configuration.Configure(hibernate.cfg.xml);
    SessionFactory sessionFactory = configuration.buildSessionFactory();
     Session  session = sessionFactory.openSession();
    
  • When we create session Object it will get the Conection With The Database.
  • Session is the main Runtime interface to do all interactions with the databases.
  • By using Session Object we can persist data to databse and we can retrive data from database.
  • Session Object will Hold The Cache(First Level Cache).
  • Session Object is Not Threadsafe.
  • Once We Close The Session by Calling Close() (i.e. session.close())Method It Closes The ConnectionWith Databse. Session Object is factory for Transactions.

Transaction (org.hibernate.Transaction)

  • Transaction Is a unit of work done in a database. Transaction based on all or nothing princliple like it executes all methods or none.
  • Hibernate trancations are maintained by Transaction managers given by JTA ,JNDI,JDBC.