Hibernate interview questions

Interview Question of hibernate:

                                         Block Diagram

The Hibernate architecture is categorized in four layer

  •  Java application layer
  • Hibernate framework layer
  • Backend  API Layer
  • Database Layer

Elements of Hibernate Architecture

For creating the first hibernate application, we must know the elements of Hibernate architecture. They are as follows:

Session Factory:

The Session Factory is a factory of session and client of ConnectionProvider. It holds second level cache (optional) of data. The org.hibernate.SessionFactory interface provides factory method to get the object of Session.

Session:

The session object provides an interface between the application and data stored in the database. It is a short-lived object and wraps the JDBC connection. It is factory of Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The org.hibernate.Session interface provides methods to insert, update and delete the object. It also provides factory methods for Transaction, Query and Criteria.

Transaction:

The transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction interface provides methods for transaction management.

ConnectionProvider:

It is a factory of JDBC connections. It abstracts the application from Driver Manager or Data Source. It is optional.

TransactionFactory:

It is a factory of Transaction. It is optional.

How hibernate ensures second level cache is updated with latest data in database?                  

Hibernate manages the cache itself , so when you update some entity through Hibernate session  it will invalidate cache entry associated with this entity- so cache is always fresh.

If another process (or even second JVM running the same hibernate application) updates the record in database, hibernate on first JVM is unaware of this this fact and has stale object in its cache,

However you can use any cache implementation you want, there are many production-ready cache providers that allow you to configure how long give entity will  be stored in cache for Example, you can configure your cache to invalidate all entities after 30 sec and so on.

Transient, persistent, and detached, Hibernate Lifecycle Of POJO Class Objects?

   Actually our POJO class object having 3 states like…

  •    Transient state    
  •    Persistent state
  •     Detached state

Transient& Persistent states:

  •  Whenever object of a pojo class is created then it will be in the Transient state.
  •  When the object is in a Transient state it doesn’t represent any row of the database, I mean not associated with any Session object, if we speak more we can say no relation with the database it’s just an normal object.
  •    If we modify the data of a pojo class object, when it is in transient state then it doesn’t effect on the database table.
  • When the object is in persistent state, then it represent one row of the database, if the object is in persistent state then it is associated with the unique Session.
  •  if we want to move an object from persistent to detached state, we need to do either closing that session or need to clear the cache of the session
  • if we want to move an object from persistent state into transient state then we need to delete that object permanently from the database

import org.hibernate.*;import org.hibernate.cfg.*;
public class  Test{
            public static void main(String[] args)            {
                        Configuration cfg = new Configuration();                        cfg.configure(“hibernate.cfg.xml”);
                        SessionFactory factory = cfg.buildSessionFactory();                        Session session = factory.openSession();
         // Transient state start                        Employee e = new Employee ();                        e.setEmployeeId(101);                        e.setEmployeeName(“iPhone”);         // Transient state end
         // Persistent state start                        Transaction transaction = session.beginTransaction();                        session.save(e);                        System. Out.println(“Object saved successfully…..!!”);                       transaction.commit();         // Persistent state end                          session.close();                        factory.close();            }
}

Note:

  • Check the above Test program, line between commented Transient state start and end just loaded the object and called the corresponding setter methods, it’s not related to the database row.
  •    if you see,  save( ) method in the Session Interface, means the object is now having the relation with the database.
  • if we want to convert the object from Transient state to Persistent state we can do in 2 ways.

Ø  By saving that object like above

Ø  By loading object from database

               if we do any modifications all the changes will first applied to the object in session cache                only . Let  we do the modifications 3 times, then 3 times we need to save the changes                      into the database right, which means number of round trips from our application to                        database will be increased, Actually if we load an object from the database, first it will                    saves in the cache-memory so if we do any number of changes all will be effected at                        cache level only and finally we can call save or update method so with the single call of                    save or update method the data will be saved into the database.

               If we want to save an object into database then we need to call any one of the following                  3 methods:

·         save()

·         persist()

·         saveOrUpdate()

               If we want to load an object from database, then we need to call either load() or get()                     methods   

            Transient:

One newly created object, without having any relation with the database, means never persistent, not associated with any Session object

             Persistent:

Having the relation with the database, associated with a unique Session object

             Detached:

Previously having relation with the database [persistent], now not associated with any Session

see the next sessions for the better understanding of the life cycle states of pojo class object(s) the hibernate