Hibernate Interview Questions And Answers:
A list of top most asked Hibernate interview questions and answers are given below
1.What is Hibernate?
Hibernate is an Object-Relational Mapping(ORM) solution for JAVA and it raised as an open source persistent framework created by Gavin King in 2001. It is a powerful, high performance Object-Relational Persistence and Query service for any Java Application.
Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieve the developer from 95% of common data persistence related programming tasks.
2.What are the advantages of using Hibernate?
Following are the advantages of using Hibernate.
Hibernate takes care of mapping Java classes to database tables using XML files and without writing any line of code.
Provides simple APIs for storing and retrieving Java objects directly to and from the database.
If there is change in Database or in any table then the only need to change XML file properties.
Abstract away the unfamiliar SQL types and provide us to work around familiar Java Objects.
Hibernate does not require an application server to operate.
Manipulates Complex associations of objects of your database.
Minimize database access with smart fetching strategies.
Provides Simple querying of data.
3.Name some of the databases that hibernate supports.
Hibernate supports almost all the major RDBMS. Following is list of few of the database engines supported by Hibernate.
HSQL Database Engine
Microsoft SQL Server Database
Sybase SQL Server
Informix Dynamic Server
4.Name some of the java based tools/frameworks that supports hibernate integration.
Hibernate supports a variety of other technologies, including the following:
6.What are the key components/objects of hibernate?
Following are the key components/objects of Hibernate:
Configuration – Represents a configuration or properties file required by the Hibernate.
SessionFactory – Configures Hibernate for the application using the supplied configuration file and allows for a Session object to be instantiated.
Session – Used to get a physical connection with a database.
Transaction – Represents a unit of work with the database and most of the RDBMS supports transaction functionality.
Query – Uses SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects.
Criteria – Used to create and execute object oriented criteria queries to retrieve objects.
7.What are the two key components of a hibernate configuration object?
The Configuration object provides two keys components:
Database Connection: This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.
Class Mapping Setup:
This component creates the connection between the Java classes and database tables.
8.What is a configuration object in hibernate?
The Configuration object is the first Hibernate object you create in any Hibernate application and usually created only once during application initialization. It represents a configuration or properties file required by the Hibernate.
9.What is a SessionFactory in hibernate?
Configuration object is used to create a SessionFactory object which inturn configures Hibernate for the application using the supplied configuration file and allows for a Session object to be instantiated. The SessionFactory is a thread safe object and used by all the threads of an application.
The SessionFactory is heavyweight object so usually it is created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file. So if you are using multiple databases then you would have to create multiple SessionFactory objects.
10.What is Session in hibernate?
A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.
The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed.
11.What is Transaction in hibernate?
A Transaction represents a unit of work with the database and most of the RDBMS supports transaction functionality. Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC or JTA).
This is an optional object and Hibernate applications may choose not to use this interface, instead managing transactions in their own application code.
12.What is Query in hibernate?
Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.
13.What is Criteria in hibernate?
Criteria object are used to create and execute object oriented criteria queries to retrieve objects.
14.What is many-to-one association?
A many-to-one association is the most common kind of association where an Object can be associated with multiple objects. For example a same address object can be associated with multiple employee objects.
element is used to define many-to-one association. The name attribute is set to the defined variable in the parent class. The column attribute is used to set the column name in the parent table.
15.What is one-to-one association?
A one-to-one association is similar to many-to-one association with a difference that the column will be set as unique. For example an address object can be associated with a single employee object.
element is used to define one-to-one association. The name attribute is set to the defined variable in the parent class. The column attribute is used to set the column name in the parent table which is set to unique so that only one object can be associated with an other object.
16.What is one-to-many association?
In One-to-Many mapping association, an object can be can be associated with multiple objects. For example Employee object relates to many Certificate objects.
A One-to-Many mapping can be implemented using a Set java collection that does not contain any duplicate element.
element of set element indicates that one object relates to many other objects.
17.What is many-to-many association?
A Many-to-Many mapping can be implemented using a Set java collection that does not contain any duplicate element.
element indicates that one object relates to many other objects and column attributes are used to link intermediate column.
18.Is SessionFactory a thread-safe object?
Yes, SessionFactory is a thread-safe and can be accessed by multiple threads simultaneously.
19,.Is Session a thread-safe object?
No, Session is not thread-safe.
20.What is the difference between save() and persist() methods of session object?
session.save saves the object and returns the id of the instance whereas persist do not return anything after saving the instance.
21.What is the difference between get() and load() methods of session object?
There are following differences between get() and load() methods.
get() returns null if no data is present where as load throws ObjectNotFoundException exception in such case.
get() always hits the database whereas load() method doesn’t hit the database.
get() returns actual object whereas load() returns proxy object.
A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.
All classes that do not extend or implement some specialized classes and interfaces required by the EJB framework.
22.What is lazy loading?
Lazy loading is a technique in which objects are loaded on demand basis. Since Hibernate 3, lazy loading is by default, enabled so that child objects are not loaded when parent is loaded.
23.What is HQL?
HQL stands for Hibernate Query Language. It takes java objects in the same way as SQL takes tables. HQL is a Object Oriented Query language and is database independent.
24.What is first level cache in hibernate?
The first-level cache is the Session cache and is a mandatory cache through which all requests must pass. The Session object keeps an object under its own power before committing it to the database.
25.What is second level cache in hibernate?
Second level cache is an optional cache and first-level cache will always be consulted before any attempt is made to locate an object in the second-level cache. The second-level cache can be configured on a per-class and per-collection basis and mainly responsible for caching objects across sessions.
26.What is Query level cache in hibernate?
Hibernate also implements a cache for query resultsets that integrates closely with the second-level cache.
This is an optional feature and requires two additional physical cache regions that hold the cached query results and the timestamps when a table was last updated. This is only useful for queries that are run frequently with the same parameters.