
Hibernate is a popular Object Relational Mapping (ORM) framework of Java. It helps in mapping the Object Oriented Domain model to Relational Database tables.
Hibernate is a free software distributed under GNU license.
Hibernate also provides implementation of Java Persistence API (JPA).
In simple words, it is a framework to retrieve and store data from database tables from Java.
Q. How will you map the columns of a DB table to the properties of a Java class in Hibernate?
We can map the class properties and table columns by using one of the two ways:
XML: We can map the column of a table to the property of a class in XML file. It is generally with extension hbm.xml
Annotation: We can also use annotations @Entity and @Table to map a column to the property of a class.
Q. Does Hibernate make it mandatory for a mapping file to have .hbm.xml extension?
No. It is a convention to have.hbm.xml extension in the name of a mapping file. It is not a requirement enforced by Hibernate. We can use any other extension of our convenience for this.
Q. What are the steps for creating a SessionFactory in Hibernate?
Steps to create a SessionFactory in Hibernate are:
Configuration: First create a Configuration object. This will refer to the path of configuration file.
Resource: Add config file resource to Configuration object.
Properties: Set properties in the Configuration object.
SessionFactory: Use Configuration object to build SessionFactory.
Egg.
Configuration config = new Configuration();
config.addResource(“testInstance/configuration.hbm.xml”);
config.setProperties( System.getProperties() );
SessionFactory sessions = config.buildSessionFactory();
Q. Why do we use POJO in Hibernate?
POJO stands for Plain Old Java Objects. A POJO is java bean with getter and setter methods for each property of the bean.
It is a simple class that encapsulates an object’s properties and provides access through setters and getters.
Some of the reasons for using POJO in Hibernate are:
POJO emphasizes the fact that this class is a simple Java class, not a heavy class like EJB.
POJO is a well-constructed class, so it works well with Hibernate proxies.
POJO also comes with a default constructor that makes it easier to persist with a default constructor.
Q. What is Hibernate Query Language (HQL)?
Hibernate Query Language is also known as HQL. It is an Object Oriented language. But it is similar to SQL.
HQL works well with persistent objects and their properties. HQL does not work on database tables.
HQL queries are translated into native SQL queries specific to a database.
HQL supports direct running of native SQL queries also. But it creates an issue in Database portability.
Q. How will you call a stored procedure in Hibernate?
Hibernate supports executing not only simple queries but also stored procedure of database. There are three ways to call a stored procedure in Hibernate:
XML mapping file:
We can declare the store procedure inside XML Mapping file. We can call it with getNamedQuery().
Native SQL: We can use Native SQL to call a store procedure query directly. In this example GetEmployees() stored procedure is being called.
Query query = session.createSQLQuery(
"CALL GetEmployees(:employeeId)")
Use annotation:
We can also mark out stored procedure with @NamedNativeQueries annotation.
//Employee.java
@NamedNativeQueries({
Dirty Checking is very useful feature of Hibernate for write to database operations. Hibernate monitors all the persistent objects for any changes. It can detect if an object has been modified or not.
By Dirty Checking, only those fields of an object are updated that require any change in them. It reduces the time-consuming database write operations.
Callback interface in Hibernate is mainly used for receiving notifications of different events from an object.
Egg. We can use Callback to get the notification when an object is loaded into or removed from database.
There are mainly two ways to configure Hibernate application:
XML based: We can define the Hibernate configuration in an XML file like ibernate.cfg.xml file
Programming based: We can also use code logic to configure Hibernate in our application.
Hibernate provides Query Cache to improve the performance of queries that run multiple times with same parameters.
At times Query Caching can reduce the performance of Transactional processing. By default Query Cache is disabled in Hibernate.
It has to be used based on the benefits gained by it in performance of the queries in an application.
Hibernate supports following four types of Association mappings:
Unidirectional association: This kind of association works in only one direction.
Unidirectional association with join tables
Bidirectional association: This kind of association works in both directions.
Bidirectional association with join tables
In Hibernate there can be following three types of Unidirectional Association mappings:
Many to one
One to one
One to many
In Hibernate, we can use one of the following options to retrieve objects from database:
Identifier: We can use load() or get() method and pass the identifier like primary key to fetch an object from database.
HQL: We can create a HQL query and get the object after executing the query.
Criteria API: We can use Criteria API to create the search conditions for getting the objects from database.
Native SQL: We can write native SQL query for a database and just execute it to get the data we want and convert it into desired object.
A Bag in Hibernate is an unordered collection. It can have duplicate elements. When we persist an object in a bag, there is no guarantee that bag will maintain any order.
A Set in Hibernate can only store unique objects. If we add the same element to set second time, it just replaces the old one. By default a Set is unordered collection in Hibernate.
In Hibernate, load() and get() methods are quite similar in functionality.
The main difference is that load() method will throw an ObjectNotFoundException if row corresponding to an object is not found in the database.
On the other hand, get() method returns null value when an object is not found in the database.
It is recommended that we should use load() method only when we are sure that object exists in database.
As a thumb rule we can follow these guidelines:
We should use get() method when we want to load an object.
We should use load() method when we need a reference to an object without running extra SQL queries.
Hibernate is one of the most popular framework for enterprise applications implemented in Java. Big companies like Amazon, Netflix, Google etc use Hibernate based architecture. This course is designed to help you achieve your goals in Hibernate field. Software Engineers with Hibernate knowledge may get more salary than others with similar qualifications without Hibernate knowledge.
In this course, you will learn how to handle interview questions on Hibernate framework in Software Design and Development. I will explain you the important concepts of Hibernate architecture.
You will also learn the benefits and use cases of Hibernate in this course.
What is the biggest benefit of this course to me?
Finally, the biggest benefit of this course is that you will be able to demand higher salary in your next job interview.
It is good to learn Hibernate for theoretical benefits. But if you do not know how to handle interview questions on Hibernate, you can not convert your Hibernate knowledge into higher salary.
What are the topics covered in this course?
We cover a wide range of topics in this course. We have questions on Hibernate best practices, Security. tricky questions etc.
How will this course help me?
By attending this course, you do not have to spend time searching the Internet for Hibernate interview questions. We have already compiled the list of the most popular and the latest Hibernate Interview questions.
Are there answers in this course?
Yes, in this course each question is followed by an answer. So you can save time in interview preparation.
What is the best way of viewing this course?
You have to just watch the course from beginning to end. Once you go through all the videos, try to answer the questions in your own words. Also mark the questions that you could not answer by yourself. Then, in second pass go through only the difficult questions. After going through this course 2-3 times, you will be well prepared to face a technical interview in Hibernate framework.
What is the level of questions in this course?
This course contains questions that are good for a Fresher to an Architect level. The difficulty level of question varies in the course from a Fresher to an Experienced professional.
What happens if Hibernate technology changes in future?
From time to time, we keep adding more questions to this course. Our aim is to keep you always updated with the latest interview questions on Hibernate.
What are the sample questions covered in this course?
Sample questions covered in this course are as follows: