Hibernate Interview Questions

1.What does ORM consists of ?

An ORM solution consists of the following four pieces:

* API for performing basic CRUD operations
* API to express queries referring to classes
* Facilities to specify meta data
* Optimization facilities : dirty checking,lazy associations fetching

2.What is Hibernate?

Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables using (XML) configuration files.Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks.

3.Why do you need ORM tools like hibernate?

The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart from this, ORM provides following benefits:

* Improved productivity
o High-level object-oriented API
o Less Java code to write
o No SQL to write
* Improved performance
o Sophisticated caching
o Lazy loading
o Eager loading
* Improved maintainability
o A lot less code to write
* Improved portability
o ORM framework generates database-specific SQL for you

4.What are the most common methods of Hibernate configuration?

The most common methods of Hibernate configuration are:

* Programmatic configuration
* XML configuration (hibernate.cfg.xml)

5.What role does the Session interface play in Hibernate?

The Session interface is the primary interface used by Hibernate applications. It is a single-threaded, short-lived object representing a conversation between the application and the persistent store. It allows you to create query objects to retrieve persistent objects.

Session session = session Factory.open Session();

Session interface role:

* Wraps a JDBC connection
* Factory for Transaction
* Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier

6.What is the general flow of Hibernate communication with RDBMS?

The general flow of Hibernate communication with RDBMS is :

* Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files
* Create session factory from configuration object
* Get one session from this session factory
* Create HQL Query
* Execute query to get list containing Java objects

7.What role does the Session Factory interface play in Hibernate?

The application obtains Session instances from a Session Factory. There is typically a single Session Factory for the whole application?created during application initialization. The Session Factory caches generate SQL statements and other mapping meta data that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work

Session Factory session Factory = configuration.build Session Factory();

8.What Does Hibernate Simplify?

Hibernate simplifies:

* Saving and retrieving your domain objects
* Making database column and table name changes
* Centralizing pre save and post retrieve logic
* Complex joins for retrieving related items
* Schema creation from object model

9.Define cascade and inverse option in one-many mapping?

cascade – enable operations to cascade to child entities.

inverse – mark this collection as the “inverse” end of a bidirectional association.
Essentially “inverse” indicates which end of a relationship should be ignored, so when persisting a parent who has a collection of children, should you ask the parent for its list of children, or ask the children who the parents are?

10.What are the benefits does Hibernate Template provide?

The benefits of Hibernate Template are :

* Hibernate Template, a Spring Template class simplifies interactions with Hibernate Session.
* Common functions are simplified to single method calls.
* Sessions are automatically closed.
* Exceptions are automatically caught and converted to runtime exceptions.