We have seen how to write a Hibernate Application, which inserts the values into DataBase Server. Now let us know what each step is doing in the background.
Step1: When we create the Configuration object a plain java object will be created. This configuration object stores all the configuration details. Initially when we create the object no data is available in that.
Step2: When we call cfg.configure() it searches for hibernate.cfg.xml file(Configuration file) in the WEB-INF folder. If the Configuration file is not found it throws an exception "org.hibernate.HibernateException/hibernate.cfg.xml not found".
- If the configuration file is found, parser will validate the xml file. If the file is not valid parser throws an exception "could not parse the xml file". If the xml file(configuration file) is valid configure() method gets the values of Driverclass, url, username, password, and the information about Hibernate Mapping Files. Then it reads the contents from Hibernate mapping files and stores the entire data into Configuration Object(JVM's Memory).
- Now the connection pool program acquires connections from DBServer. Now, buildSessionFactory() picks the connection from the connection pool and establish the connection with DBServer.
- Now it will check weather the required tables are available or not. If the tables are not available buildSessionFactory() creates the table.
- It is the responsibility of the buildSessionFactory() to create and process all the query operations(CURD operations) for each table. After processing the buildSessionFactory() returns the connection to the connection pool.
- As the buildSessionFactory method is doing more work, it is recommended to call this method only once in the life cycle of the project.
Step4: When we open the session object by using sf.openSession().
- Hibernate creates an object which is similar to hashtable object. We call this object as First Level Cache.
- Hibernate associates this object with Session Object.
- This First level cache is available until the Session object is closed. Only the session object can access the data from First Level Cache.
- Opening the session object is nothing but getting the connection object. This method is not an expensive operation. It is always recommended to open a new session object whenever it is required.
Step6: Create POJO class object corresponding to the table. When the object is created all the instance variables are initialized with default values.
Fig: POJO Class object in which values are initialized with null values |
Fig: variables initialized with values |
- Ex: hsession.contains(e);
Step9: When we call tx.commit(), hibernate checks for any objects in the First Level Cache which are waiting for an operation to be taken care. If the objects are available hibernate finds, to which POJO class these objects belongs to(Hibernate internally uses getClass() to find the class).
- Now hibernate finds to which table this POJO class is mapped, and from that table it gets the registration code. Based on this code it has got the query form the JVM's memory. Now hibernate get the data from the POJO class and supply as the input to the query. This query is added to batch object and send to the DBServer.
- By default hibernate will not display the query sent to the DB. If we want to see the query we have to add a property in the hibernate configuration file. The following is the property to show SQL queries.
<property name="show-sql">true</property>
Step10: When we call hsession.close(), the connection is returned to the connection pool as well as it removes the First Level Cache object.
Step11: When we call sf.close(), it removes second level cache as well as returns all the connections from the connection pool to DBServer.
Very good and clear explanation... thank you...
ReplyDeleteIt is very accurate sir,,, nice work...
ReplyDelete