Hibernate Interview Questions



1) What is Hiberante?
Hibernate is a free, open source Java package that makes it easy to work with relational databases. It is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects(POJOs) to relational database tables using (XML) configuration files. It's purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks.

2) What is ORM?
ORM stands for Object relational mapping. Object-relational mapping is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" which can be used from within the programming language.

3) What is the need of Hibernate?
To overcome the mismatch of Object oriented data and table oriented data base. It improves the productivity by reducing java code.

4) How will you configure Hibernate?
1. configuration file hbm.cfg.xml(or hibernate.properties)
This file contains the connection driver class, connection URL, connection username, connection password, dialect etc
2. Plain old java object(POJO)
for example Customer table is there with coloumns firstname,lastname,address then corresponding customer class should be configured.
3. mapping file(*.hbm.xml).These files are used to map persistant objects to a relational database.
for example Customer class, customer.hbm.xml mapping file exists.

5) What are the core interfaces in Hibernate framework?
There are 5 core interfaces in Hibernate. They are as follows:
1.SessionFactory interface
2.Session interface
3.Configuration interface
4.Transaction interface
5.Query and Criteria interfaces

6) Explain about SessionFactory interface in Hibernate?
SessionFactory is a single datastore created while application initialization. SessionFactory is threadsafe i.e. many threads can access it concurrently and request for sessions. It holds cached data that has been read in one unit of work and may be reused in a future unit of work.
SessionFactory sessionFactory = configuration.buildSessionFactory();

7) Explain about Session interface in Hibernate
Session interface is a single threaded object which represents single unit of work with application and persistent database. The main role of session interface is it wraps a JDBC connection.
Session session = sessionFactory.openSession()

8) What is the difference between session.get() and session.load() methods?
Both the methods create a persistent object by loading the required object from the database.Session.load() method throws an exception if there was no such object but session.get() returns null.

9) What is Hibernate Query Language(HQL)?
Hibernate is equipped with an extremely powerful query language that looks very much like SQL. HQL is fully object-oriented. HQL uses classes and properties instead of tables and columns.

10) Explain the steps involved in Hibernate's communication with the database?
1. When application intializes loads the hibernate.cfg.xml file. It will contain configuration object and hbm mapping files.
2. Create session factory from configuration object.
3. Create session from this session factory.
4. Create HQL(Hibernate Query Language Query) query.
5. Execute query to get list containing java objects.