Friday, April 7, 2017

Hibernate Interview Questions

1. What is the difference between openSession and getCurrentSession?

         openSession will open a new session and getCurrentSessiona will use an existing one which is in available in context.

2. What is the difference between get/load and createCriteria?   

         load will not hit database(because of fake object) and it will throw object not found exception if data not available.

         get will hit database and it will throw null if data is not available.

       createCriteria is a method of session interface and it will create a criteria object of persistence object of you requested class.

e.g : Criteria cr  = session.createCriteria(Employee.class)

3. Why we need to use hibernate ?

        1. HQL - You do not want to write query and it will be suitable to all database when we change in future.

        2. OOPS concept -   You will get oops concept since you are using table as entity in hibernate. So inheritance, encapsulation etc., all you will get.

         3. Caching Mechanism - First level cache and second level cache possible.

         4. Lazy loading - You can set when child has to be loaded while parent loading.

4. How many ways configure hibernate.config.xml file ?

        1. By using xml file (hibernate.config.xml)
        2.By using setProperty() method of configuration
        3.By using properties file

5. How you do hibernate pagination ?

    You can set the result per page wise. 

        Query query = session.createQuery("From Person");
        query.setFirstResult(0);
        query.setMaxResults(10);

ScrollableResults also we can use for pagination.

For more please check this stackoverflow link.

6. What is hibernate First level cache ?

1. First level cache is associated with session object.
2. Default it will enable and you can not disable it.
3. When we query for first time, it will get from DB and second time if you excecute the same query it will get from cache.

7. What is second level cache ?

Second level cache is associated with session Factory object. Actually it will reduce our database traffic. We have lot of option to enable second level cache in hibernate. But mostly all will use EHCache, since its fast and lightweight. It will support read only , read/write operation 

8. How to enable second level cache in hibernate ?

You can enable it through hibernate.cfg.xml. It look likes below.


<hibernate-configuration>
      <session-factory>
          <property  name=hibernate.cache.provider_class">
                     org.hibernate.cache.EHCacheProvider
          </property>
          <property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
      </session-factory>
</hibernate-configuration>

9. What is evict method in hibernate ?

evict method will be useful, to remove session from first level cache.

Person t = (Person) session.load(Person.class, new Integer(1));
   
session.evict(t);

It will remove only t cached from particular session . But session.clear() remove all cached.

10. What is hibernate versioning ?

If you want to find how many times your Object was modified ,  then you should apply versioning in hibernate. Whenever you modified the object it will increment one number automatically.

11. How do you call aggregate functions in hibernate?

String hql = "select count(name) from Product";

Query query = session.createQuery(hql);
List listResult = query.list();

You can use all sum(), avg(), min(), max() etc.,

12. How to disable Hibernate first level cache ?

We can not disable hibernate first level cache and it will be enabled by default.

13. What are the interfaces available in hibernate ?

Session Interface

Session Factory Interface

Configuration Interface

Transaction Interface

Query and Criteria Interface