Top 100 Hibernate Interview Questions And Answers

Hibernate Interview Questions And AnswersHibernate is a popular and widely adopted ORM framework in the Java ecosystem. Many organizations use Hibernate extensively for data persistence and database interactions in their Java applications. By acquiring Hibernate skills, you align yourself with industry standards and increase your employability in Java development roles.

Hibernate is widely used in enterprise-level Java applications. As long as Java remains a popular programming language, the demand for Hibernate skills will continue. Many organizations across various industries rely on Hibernate to handle their database interactions efficiently.

In an interview, the recruiter may ask technical interview questions with behavioral questions aimed at assessing your knowledge, skills, and experience. So, if you are going for a Java developer interview, don’t forget to prepare the most important Hibernate interview questions and answers.

Answer:

Hibernate is a Java-based ORM tool that renders a framework for mapping application domain objects to relational database tables & vice-versa. Hibernate persistence API is useful for CRUD operations. It provides an option to map old Java objects to traditional database tables using JPA annotations & XML-based configuration. It also develops persistent logic that stores & processes the data for longer use.

Answer:

It is a technique that maps the object stored in the database. An ORM helps to simplify data creation, access, & manipulation. It internally uses the Java API to interact with the databases.

Answer:

There are five core interfaces used in every Hibernate application, which are given below:

  • Session interface- It is the primary interface used by Hibernate applications. The session interface has a lightweight & inexpensive instance to destroy & create.
  • SessionFactory interface- This is a factory interface that delivers the session objects to Hibernate applications. Usually, there is only one SessionFactory for an entire application shared amongst all the application threads.
  • Configuration interface- As the name suggests, this interface is used to configure & bootstrap Hibernate. The instance of the Configuration interface is used by the applications to indicate Hibernate-specific mapping documents’ location.
  • Transaction interface- It is an optional interface, but the above three interfaces are mandatory in all Hibernate applications. Transaction interface abstracts the code from any transaction implementation like JDBC transaction.
  • Query & criteria interface- This interface allows the user to perform queries & controls the query execution’s flow.

Answer:

The major advantages of using ORM over JDBC are as follows:

  • ORM enables business code access to the objects rather than database tables.
  • ORM hides the details of SQL queries from OO logic.
  • It eliminates the need to deal with database implementation.
  • Entities are based on business concepts rather than database structures.
  • ORM generates automatic keys & transaction management.
  • It accelerates application development.
  • ORM improves portability as it generates database-specific SQL.

Answer:

The following are the databases supported by Hibernate:

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

Answer:

  1. Save() & persist() methods are used for saving an object in the database.
    Sr. No. Key save() persist()
    1 Basic  Save() stores object into the database. Persist() also stores the object into the database.
    2 Return Type It returns generated id & its return type is serializable. It returns nothing as it has a void return type.
    3 Transaction Boundaries It can save objects within & outside the boundaries. It can only save the object within the transaction boundaries.
    4 Detached Object It creates a new row in the table for the detached object. It throws a persistence exception for the detached object.
    5 Supported by Save() is only supported by the Hibernate. Persist() is also supported by the JPA.

Answer:

  1. Following are the key differences between the get() and load() methods:
    • get() returns null if no data is found, whereas load throws ObjectNotFoundException exception in such a case.
    • get() method always hit the database, while the load() method doesn’t hit the database.
    • get() method returns actual object, but load() method returns proxy object.

Answer:

  1. Below are the differences between the update() and merge() methods:
    No. The update() method merge() method
    1 ) Update implies editing something. Merge refers to combine something.
    2 ) You should use the update() method if the session doesn’t have an already persistent state with the same id. It means that the update should only be used within the session as after closing the session, it will throw the error. You should use the merge() method if you don’t know the session’s state & you want to modify at any time.

Answer:

An object in Hibernate exists in the following states:

  • Transient: The transient state depicts new objects created in Java but not associated with the Hibernate session.
  • Persistent: The persistent state represents objects associated with the Hibernate session.
  • Detached: The detached state describes an object formerly persistent & associated with the Hibernate session.

Answer:

Four types of association mapping are possible in Hibernate:

  • One to One;
  • One to Many;
  • Many to One, and;
  • Many to Many.

Answer:

Lazy loading helps to improve the performance. It enables you to load the child objects on demand. The lazy loading has been enabled by default since Hibernate 3; thus, one doesn’t need to do lazy=”true.”

Answer:

  1. HQL stands for (Hibernate Query Language). It is a robust object-oriented query language similar to SQL or Structured Query Language. Below are the benefits of HQL over SQL:
    • No need to learn SQL;
    • HQL is database independent and;
    • It is simple to write a query in it.

Answer:

Hibernate has two types of caches; first level & second-level cache, which differs in the following ways:

First Level Cache Second Level Cache
The first level cache is local to the session object & it cannot be shared amongst multiple sessions. The second-level cache is maintained at a SessionFactory level & it can be shared amongst all Hibernate sessions.
  Since the first level cache is enabled by default, so there is no way to disable it. The second-level cache is disabled by default; however, we can enable it by configuration.
It is available only until the session is open, so once the session is closed, the first level cache is destroyed. It is available through an application’s life cycle & can only be destroyed & recreated when an application is restarted.

Answer:

Below are some advantages of Hibernate:

  • It is fast, lightweight, and open-source;
  • Hibernate reduces code length, removes boilerplate codes, & frees up developers for other tasks;
  • It reinforces the object-level relationship;
  • It generates independent database queries;
  • Hibernate provides resources for automatically creating tables;
  • It is easy to integrate with other (EE) Enterprise Edition frameworks in Java.

Answer:

Following are some important reasons to use the Hibernate framework over JDBC :

  • Hibernate overcomes the database dependency you face in the JDBC.
  • When working on JDBC, it costs heavily to change databases; however, hibernate overcomes this problem with flying colours.
  • Code portability isn’t an option while working on the JDBC, but Hibernate can easily handle it.
  • Hibernate reinforces & strengthens the object-level relationship.
  • Hibernate overcomes the exception handling that is mandatory when working on JDBC.
  • Hibernate reduces the code’s length & increases readability by overcoming the boilerplate issue.

Answer:

  • Hibernate uses (HQL) Hibernate Query Language that makes it database-independent;
  • It supports auto DDL operations;
  • Hibernate also has Auto Primary Key Generation support;
  • It supports the cache memory;
  • Exception handling is not mandatory for Hibernate.
  • Hibernate is the most important ORM tool.

Answer:

A one-to-one association is just like a many-to-one association with one difference that the column will be set as unique. The element helps to define one-to-one association. The name attribute is set to a defined variable in a parent class. The column attribute sets the column name in the parent table, which is then set to unique so that only an object can be associated with another object.

Answer:

In One-to-Many association, an object can be associated with multiple objects. It is implemented through a set Java collection that does not have any redundant elements. The One-to-Many element indicates the relationship of an object to multiple objects.

Answer:

Many-to-many association mapping requires one entity attribute along with @ManyToMany annotation. It can be bidirectional or unidirectional. In Unidirectional, the attributes model the association and allow you to navigate it in your domain model. The annotation informs about the Hibernate to map a Many-to-Many association. In bidirectional, the mapping enables you to navigate the association in both directions.

Answer:

Follow these simple steps to integrate Spring in Hibernate:

  • First, create a table in the database (optional)
  • Thereon, create an applicationContext.xml file that contains information on SessionFactory, DataSource, etc.
  • The next step is to create an Employee.java file which is a persistent class.
  • Followed by creating an employee.hbm.xml file (mapping file).
  • Now you must create an EmployeeDao.java file that uses HibernateTemplate.
  • Lastly, create an InsertTest.java file that will call methods of the EmployeeDao class.