Monday, February 20, 2017

Hibernate Interview Questions

In this post I am sharing frequently asking Hibernate Interview Questions and answers.Hibernate is a pure Object Relational Mapping(ORM) and persistence framework. It allows you to map plain old java objects to relational database tables using XML configuration files. Hibernate is ambitious project that aims to be a complete solution to the problem of managing persistent data in Java. Hibernate integrates smoothly with most new and existing applications and does not require disruptive changes to the rest of the application.Let us see some of the frequently asking interview questions in Hibernate.


1) What is Hibernate?

Ans:  Hibernate is a popular framework of  java that aims to be a complete solution to the problem of managing persistent data. It allows you to map plain old java objects to relational database tables using XML configuration files. After java objects mapping to database tables,database is used and handled using java objects without writing complex database queries.

2) Why Hibernate?

Ans: All most all applications require persistent data. When we talk about persistence in java,we are normally talking about storing  data in relational database using SQL. In java data is stored in the form of object. Hibernate is also one of the Object relational mapping framework.After java objects mapping to database tables,database is used and handled using java objects without writing complex database queries.

ORM provides the following benefits:

Improved productivity: High-level object-oriented API,less java code to write,No SQL to write
improved performance:  Lazy loading,sophisticated caching,eager loading
improved maintainability: A lot less code to write
improved portability: ORM generates database specific  SQL for you

3) What is ORM?

Ans: ORM stands for Object Relational Mapping. It is the fundamental concept of Hibernate framework which maps database between tables with java objects and then provides various API's to perform different types of operations on the tables.

4) What does ORM consists of?

Ans:  The following are the ORM solutions:
  • API for performing basic CURD operations
  • API to express queries referring to classes 
  • Facilities to specify metadata
  • Optimization facilities,dirty checking,lazy associations fetching



5) What are the benefits of Hibernate over JDBC?

Ans: The following are some of the benefits of Hibernate over JDBC:


  • Using Hibernate Developer doesn't need to be expert of writing complex queries. HQL simplifies query writing process while in case of JDBC, developer has to write and tune queries.
  • In case of Hibernate, there is no need to create Connection pools as hibernate does all connection handling automatically. In case of JDBC,developer manually need to create connection pool.
6) What are the Core interfaces of Hibernate?

Ans: The following are the Core interfaces of Hibernate:


  1. Configuration
  2. SessionFactory
  3. Session
  4. Query
  5. Criteria
  6. Transaction
7) What is the most comman methods of hibernate configuration?

Ans:  The most common methods of hibernate configuration are:

1) XML configuration(hibernate.cfg.xml)
2) Programmatic Configuration

8) What is the use of configuration interface in Hibernate?

Ans: Configuration interface of hibernate framework is used to configure hibernate. It's also used to bootstrap hibernate. Mapping documents of hibernate are located using this interface.

9) What is pojo?

Ans: POJO stands for Plain Old Java Objects. These are java beans with proper getter() and setter() methods for each and every properties. Use of POJO instead  of simple java classes results in an efficient and well constructed code.

10) What is HQL?

Ans: HQL is a query language used in Hibernate which is an extension of SQL. It is every efficient,simple and flexible query language to do various type of operations on relational database without writing complex queries.

11) How do you map java objects with Database tables?

Ans: The following steps you must follow to map java objects with Database tables:

1) First we need to write a java domain objects that means setter() and getter() methods
2) Now write hbm.xml, where we map java class to table and database columns to java class variables.




12) What is the general flow of Hibernate Communication with RDBMS?

Ans:  The following steps the flow of hibernate communicate with RDBMS:

1) First Load the Hibernate Configuration file and create Configuration object. It will automatically load all hbm mapping files.
2)Create a Session Factory from Configuration object
3)Now get one session from this Session Factory
4)Create  HQL Query
5)Execute query to get list containing java objects.

13) What are the states of object in hibernate?

Ans: There are 3 states in hibernate. They are:

Transient: The object is in transient state if if is just created but has no primary key and not associated with session

Persistent: The object is in persistent state if session is open,and you just saved the instance in the database or retrieved the instance from the database.

Detached: The object is in detached state is session is closed. After detached state, object comes to persistent state,object comes to persistent state if you call lock() or update() method.

14) How do we create Session Factory in Hibernate?

Ans:  To create a Session Factory in Hibernate,an object of Configuration is created first which refers to the path of configuration file and then for that configuration,session factory is created as given below:

Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();

15)What is the difference between session.save() and session.saveOrUpdate() methods in hibernate?

Ans: Hibernate executes SQL Statements asynchronously. An insert statement is not usually executed when the application calls Session.save()

Session.save(): This method saves a record only if it is unique with respect to its primary key and will fail to insert if primary key already exists in the table.

Session.saveOrUpdate():  This method inserts a new record if primary key is unique and will update an existing record if primary key exists in the tables already.

16) What is transaction management in Hibernate?

Ans: Transaction Management is the process of managing a set of statements or commands. In hibernate,transaction management is done by transaction interface as shown in below code:

Session s=null;
Transaction tran=null;
try{
s=SessionFactory.openSession();
tran=s.beginTransaction();
doTheActions(s);
tran.commit();
}
catch(RuntimeException exe)
{
tr.Rollback();
}
finally
{
s.close();
}

17) What is the use of session.lock() in Hibernate?

Ans: This method is used to reattach an object which has been detached earlier.

18) Does Hibernate supports polymorpism?

Ans: YES. Hibernate supports polymorphism. It's queries and associations are supported in all mapping strategies of hibernate.

19) What is difference between merge and update?

Ans: if you are sure that the session does not contain an already persistent instance with the same identifier then use update() method.

If you want to merge your modifications at any time without consideration of the state of the session then use merge() method.

20) difference between get() and load()?

Ans:
  •  get() method returns null if object is not found whereas load() method  throws ObjectNotFoundException if object is not found
  • get() method always hit the database whereas load() method does not hit the database
  • get() method returns real objects not proxy whereas load() method returns proxy object
21) How to make a immutable class in hibernate?

Ans: If you make a class as mutable="flase",class will be treated as an immutable class. By default,it is mutable="true".

22)How many types of association mapping are possible in hibernate?

Ans: There are 4 types of association mapping in hibernate. They are

1) One to Many
2)Many to One
3)Many to Many

23)Is it possible to perform collection mapping with One-to-One and Many-to-One?

Ans: No, collection mapping can only be performed with one-to-many and many-to-many

24) What is Lazy Loading?

Ans: It improves the perfromance. It loads the child objects on demand. The lazy loading is enabled by default,you don't need to do lazy="true". It means not to load the child objects when parent is loaded.

25)Difference between first level cache and second level cache?

Ans: First Level Cache is associated with Session and it is enabled by default whereas Second level cache is associated with SessionFactory and it is not enabled by default.

Watch the Video for More details: Hibernate Interview Questions

Recommended to Read:

Struts Interview Questions
JDBC Interview Questions
Servlets Interview Questions
JSP Interview Questions
MVC Architecture in Java


No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...