Top 50 Hibernate Interview Questions & Answers
Top 50 Hibernate Interview Questions & Answers
com/
Hibernate is a popular framework of Java which allows an efficient Object Relational mapping
using configuration files in XML format. After java objects mapping to database tables, database
is used and handled using Java objects without writing complex database queries.
2. What is ORM?
ORM (Object Relational Mapping) is the fundamental concept of Hibernate framework which
maps database tables with Java Objects and then provides various API's to perform different
types of operations on the data tables.
3. How properties of a class are mapped to the columns of a database table in Hibernate?
Mappings between class properties and table columns are specified in XML file as in the below
example:
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.
5. How can we use new custom interfaces to enhance functionality of built-in interfaces
of hibernate?
We can use extension interfaces in order to add any required functionality which isn't supported
by built-in interfaces.
6. Should all the mapping files of hibernate have .hbm.xml extension to work properly?
1/7
https://career.guru99.com/
No, having .hbm.xml extension is a convention and not a requirement for hibernate mapping file
names. We can have any extension for these mapping files.
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
in the example below:
[crayon-5f2d2ae7a4f87799958487/]
8. What are POJOs and what's their significance?
POJOs( Plain Old Java Objects) are java beans with proper getter and setter methods for each
and every properties.
Use of POJOs instead of simple java classes results in an efficient and well constructed code.
9. What's HQL?
HQL is the query language used in Hibernate which is an extension of SQL. HQL is very
efficient, simple and flexible query language to do various type of operations on relational
database without writing complex database queries.
Criteria is a simple yet powerful API of hibernate which is used to retrieve entities through
criteria object composition.
2/7
https://career.guru99.com/
3/7
https://career.guru99.com/
[crayon-5f2d2ae7a4f96859545082/]
24. What the two methods are of hibernate configuration?
We can use any of the following two methods of hibernate configuration:
a. XML based configuration ( using hibernate.cfg.xml file)
b. Programmatic configuration ( Using code logic)
4/7
https://career.guru99.com/
Database primary key is specified in the configuration file hbm.xml. Generator can also be used
to specify how primary key is being created in the database.
In the below example, deptId acts as primary key:
Objects which have been detached and are no longer associated with any persistent entities
can be reattached by calling session.merge() method of session class.
31. What are different ways to disable hibernate second level cache?
Hibernate second level cache can be disabled using any of the following ways:
a. By setting use_second_level_cache as false.
b. By using CACHEMODE.IGNORE
c. Using cache provider as org.hibernate.cache.NoCacheProvider
5/7
https://career.guru99.com/
3. Select Fetching
4. Sub-select Fetching
38. How mapping of java objects is done with database tables?
To map java objects with database tables, we need to have Java beans properties names same
as column names of a database table. Then mapping is provided in hbm.xml file as given below:
40. What is meant by a Named SQL Query in hibernate and how it's used?
Named SQL queries are those queries which are defined in mapping file and are called as
required anywhere.
For example, we can write a SQL query in our XML mapping file as follows:
[crayon-5f2d2ae7a4f9c986847649/]
41. What's the difference between load() and get() method in hibernate?
Load() methods results in an exception if the required records isn't found in the database while
get() method returns null when records against the id isn't found in the database.
So, ideally we should use Load() method only when we are sure about existence of records
against an id.
6/7
https://career.guru99.com/
7/7
Powered by TCPDF (www.tcpdf.org)