Top 25 Hibernate Interview Questions

Hibernate is one of the most popular and widely used ORM tools for Java applications. It is the backbone in millions of enterprise applications for database operations Whether you are fresher or experience. Having good knowledge or Hibernate ORM tool helps in cracking the interview.

1) What is Hibernate?

  • We can categorize Hibernate under the Object Relational Mapping (ORM), which features the mapping of Java classes to data tables and mapping from Java data types to SQL data types.
  • Hibernate is based on Java Virtual Machine and written in Java.
  • Hibernate provides data query and retrieval facilities.
  • Hibernate recommended in enterprise applications for database operations.
  • It is one of the highly used ORM tools for Java applications.

2) What is Query level cache in Hibernate?

We can implement a cached query in Hibernate that results in sets and integrates tightly with the second-level cache. But it is optional, and it requires two cache regions that can hold the cached query results and also the timestamps whenever a table is updated. We can use it only for the queries that run frequently containing the same parameters.

<property name="hibernate.cache.use_query_cache">True</property>
Query query = session.createQuery("from Employee");
query.setCacheable(true);
query.setCacheRegion("ALL_EMP");

3) Which annotation should be used to declare a class as a Hibernate bean?

We can use @Entity annotation to declare a class as an entity.
Example:
@Entity
@Table(name=”post”)
public class Post{
String title;
String description;
}

4) What is Lazy loading in Hibernate?

In lazy loading, we can load objects on requirement basis. Lazy loading is by default enabled from Hibernate 3.0 so that the child objects not charged while the parent is loaded.

5) What is the first level cache in Hibernate?

It is session cache and mandatory cache. It is from first level cache through which all the requests must pass. The session object stores an object under its control before committing it to the database.

6) What are Inheritance Mapping Strategies available in Hibernate?

Hibernate has three ways of inheritance mapping, and They are as follow

  1. Table per concrete class
  2. Table per hierarchy
  3. Table per subclass

7) Describe the Criteria object in Hibernate?

We can use criteria objects to create and execute object-oriented Queries to retrieve the objects.

8) Explain Session object in Hibernate?

It is used to get a connection with a database. A session object is created to instantiate an interaction with the database, whereas the persistent objects are retrieved using a session object. The session objects are not thread-safe so that they be created and destroyed as per the requirement.

9) What is a One-to-One association in Hibernate?

  • A one to one association is similar to many to one association, and only one difference is that it must be set as a unique one. We can use many to one element to define one to one association.
  • A name attribute must set in the parent class, and the column attribute is used to set a column name in the parent table for the defined variable. It is unique so that only one object gets associated with another.

10) What is the use of Hibernate Session merge() call?

We can use the Hibernate Session merge() to update existing values; however, this method creates a copy from the passed entity object and returns it. The returned object is part of the persistent context and tracked for any changes, given object is not tracked. For example, program, read Hibernate merge.

11) What will happen if a user doesn’t have no-args constructor in Entity bean?

Hibernate uses Reflection API to create an instance of Entity beans while calling get() or load() method. The method Class.newInstance() is used for this, and it requires no-args constructor. So if you won’t have no-args constructor in entity beans, hibernate will fail to instantiate it and you will get HibernateException.

12) What are the collection types in Hibernate?

The Five collection types in hibernate used for one-to-many relationship mappings are as follows:

  • Bag
  • Set
  • List
  • Array
  • Map

13) How to implement relationships in hibernate?

We can quickly implement relationships like One to One Mapping, One to Many Mapping and Many to Many Mappingusing JPA annotations as well as XML based configurations.

14) How to use JNDI DataSource with Hibernate framework?

It’s always useful to allow servlet container to manage the connection pool as the user so that we define JNDI resource for DataSource, and we can use it in the web application. It’s straightforward to use in Hibernate, all we need is to remove all the database-specific properties and use below feature to provide the JNDI DataSource name.

<property name="hibernate.connection.datasource">java:comp/env/jdbc/MyLocalDB</property>

15) Which design patterns are used in Hibernate?

The design patterns which are used in Hibernate Frameworks are as described below.

  • Domain Model Pattern – An object model of the domain that incorporates both behaviour and data.
  • Data Mapper – It is a layer of Mappers that moves data between database and objects and keeping them independent of each other and the mapper itself.
  • Proxy Pattern for lazy loading
  • Factory pattern in SessionFactory

16) What are the benefits of Hibernate Tools Eclipse plugin?

Hibernate Tools plugin helps the users in writing hibernate configuration and mapping files quickly. The primary benefit is the content assist in assisting the user with different properties and XML tags to use. It validates them against the Hibernate DTD files, so the user knows any mistakes beforehand.

17) Explain the Key components of Hibernate?

The Key components of Hibernate are:

  • Session: Session is used to get a physical network with a database.
  • Query: It uses SQL and HQL string to retrieve the data from the database and create objects.
  • Transaction: Transaction represents the unit of work with a database.
  • Criteria: The primary use of criteria is to create and execute object-oriented queries and retrieve the objects.
  • Configuration: It represents the properties of files required by Hibernate
  • Session Factory: It configures hibernate for the application using the provided configuration file and instantiates the session object.

18) What are some of the databases that Hibernate supports?

Hibernate supports all the major RDMS. The list of database engines supported by Hibernate are as follows :

  • HSQL Database Engine
  • DB2/NT
  • Oracle
  • Microsoft SQL Server Database
  • Sybase SQL Server
  • Informix Dynamic Server
  • MySQL
  • PostgreSQL
  • FrontBase

19) Is it possible to run a native SQL query in hibernate?

We can execute native SQL query in hibernate with the help of SQLQuery object, But We should avoid this approach because we cannot use the benefits of hibernate first-level caching and hibernate association.

20) How to generate a log file for hibernate generated SQL queries?

We can use the following property for generating a log for hibernate

<property name="hibernate.show_sql">True</property>


We should not use this approach for a production environment, and it is only for a testing environment

21) How to connect Hibernate with Struts2 or Servlet web applications?

Hibernate integration with Struts2 or Servlet needs to be done using ServletContextListener.

22) What are the main benefits of Hibernate Tools Eclipse plugin?

Hibernate Tools plugin helps us in mapping files and writing hibernate configuration quickly. The primary benefit is the content assist, which helps us with properties or XML tags to use. It also validates them against the Hibernate DTD files, so we know any mistakes beforehand.

23) What is the main benefit of native SQL query support in hibernate?

When we want to execute database-specific queries that are not supported by Hibernate API such as the CONNECT keyword in Oracle Database and query hints At that time Native SQL query should be used.

24) Should We make Entity Class final or not? No then Why?

Hibernate use proxy class for lazy loading of data, and it can be done by extending the entity bean if we finalize lazy loading, then lazy loading won’t be possible, hence low performance.

25) SessionFactory of Hibernate is thread-safe?

Session factory of Hibernate is thread-safe because the Internal state of SessionFactory is immutable, and Multiple threads can access Session factory simultaneously to get Session instances.

That is all for Hibernate Interview Questions and Don’t worry if you were not able to answer some tricky questions, but make sure that you face it with confidence as a fresher If I have missed any vital question here, and I will add that to the list.

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
How to force HTTPS with the .htaccess file
How to Force HTTPS With the .Htaccess File

What is HTTPS? Https stands for Hyper Text Transfer Protocol Secure. We can force HTTPS with the .htaccess file if...

Close