Hibernate Interview Questions and answer

Hibernate Interview Questions and answer

We discuss Hibernate Interview Questions and answer Hibernate is a Java framework that simplifies the development of Java application to interact with the database. It is an open source, lightweight, ORM (Object Relational Mapping) tool. Hibernate implements the specifications of JPA (Java Persistence API) for data persistence. Moreover it is one of the most widely used ORM tool for Java applications. So it’s used a lot in enterprise applications for database operations. So here are some important and frequently asked Hibrenate Interview Questions and answer to give you a quick brush up before your interview.

Hibernate Interview Questions and answer

1. What are the major advantages of Hibernate Framework? 

  • It is open-sourced and lightweight.
  • Performance of Hibernate is very fast.
  • Helps in generating database independant queries.
  • Provides facilities to automatically create a table.
  • It provides query statistics and database status.

2. What is an ORM tool?

An ORM tool simplifies the data creation, data manipulation and data access. Moreover, it is a programming technique that maps the object to the data stored in the database. In addition to this the ORM tool internally uses the JDBC API to interact with the database. So it is a programming technique that maps the object to the data stored in the database.

3. Why use Hibernate Framework?

Hibernate overcomes the shortcomings of other technologies like JDBC.

  • It overcomes the database dependency faced in the JDBC.
  • Changing of the databases cost a lot working on JDBC, hibernate overcomes this problem with flying colors.
  • Code portability is not an option while working on JDBC. This is easily handled by Hibernate.
  • Hibernate strengthens the object level relationship.
  • It overcomes the exception-handling part which is mandatory while working on JDBC.
  • It reduces the length of code with increased readability by overcoming the boilerplate problem.

4. What is HQL?

HQL is the acronym of Hibernate Query Language. It is an Object-Oriented Query Language and is independent of the database.

5. How to achieve mapping in Hibernate?

Association mappings are one of the key features of Hibernate. Also, it supports the same associations as the relational database model. They are:

  • One-to-One associations
  • Many-to-One associations
  • Many-to-Many associations

You can map each of them as a uni- or bidirectional association. 

6. What is One-to-One association in Hibernate?

In this type of mapping,  you only need to model the system for the entity for which you want to navigate the relationship in your query or domain model. You need an entity attribute that represents the association, so annotate it with an @OneToOne annotation.

7. How to integrate Hibernate and Spring?

Spring is also one of the most commonly used Java frameworks in the market today. So basically Spring is a JavaEE Framework and Hibernate is the most popular ORM framework. That is why Spring Hibernate combination is used in a lot of enterprise applications. 

Following are the steps you should follow to integrate Spring and Hibernate.

  1. Add Hibernate-entity manager, Hibernate-core and Spring-ORM dependencies.
  2. Create Model classes and corresponding DAO implementations for database operations. The DAO classes will use SessionFactory that will be injected by the Spring Bean configuration.
  3. Note that you don’t need to use Hibernate Transaction Management, as you can leave it to the Spring declarative transaction management using @Transactional annotation.

8. What is a Hibernate Template class?

When you integrate Spring and Hibernate, Spring ORM provides two helper classes – HibernateDaoSupport and HibernateTemplate. After all The main reason to use them was to get two things, the Session from Hibernate and Spring Transaction Management. However, from Hibernate 3.0.1, you can use the SessionFactory getCurrentSession() method to get the current session. The major advantage of using this Template class is the exception translation but that can be achieved easily by using @Repository annotation with service classes.

9. Define Hibernate Validator Framework

Data validation is considered as an integral part of any application. Also, data validation is used in the presentation layer with the use of Javascript and the server-side code before processing. It occurs before persisting it in order to make sure it follows the correct format. Validation is a cross-cutting task, so we should try to keep it apart from the business logic. Moreover this Hibernate Validator provides the reference implementation of bean validation specs.

10. What is Transaction Management in Hibernate? How does it work?

Transaction Management is a property which is present in the Spring framework. So now, what role does it play in Hibernate?

Transaction Management is a process of managing a set of commands or statements. Moreover, in hibernate, Transaction Management is done by transaction interface. In addition to this, it maintains abstraction from the transaction implementation (JTA, JDBC). A transaction associates with Session and instantiates by calling session.beginTransaction().

11. Difference between get() vs load() method in Hibernate?

This is one of the most frequent Hibernate Interview Question. The key difference between the get() and load() method is:

load(): It will throw an exception if an object with an ID pass to them is not present.
get():  Will return null.

load(): It can return proxy without hitting the database unless required.
get(): It always goes to the database.

So sometimes using load() can be faster than the get() method.

12. What are the best practices that Hibernate recommends for persistent classes?

  • All Java classes that will persist need a default constructor.
  • All classes should contain an ID in order to allow easy identification of your objects within Hibernate and the database. This property maps to the primary key column of a database table.
  • All attributes that will persist should be declare private and have getXXX and setXXX methods defined in the JavaBean style.
  • 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.

13. What are the best practices to follow with Hibernate framework?

  • Always check the primary key field access, if it generates at the database layer then you should not have a setter for this.
  • By default hibernate set the field values directly, without using setters. So if you want Hibernate to use setters, then make sure proper access is define as @Access(value=AccessType.PROPERTY).
  • If access type is property, make sure annotations are use with getter methods and not setter methods. Avoid mixing of using annotations on both filed and getter methods.
  • Use native sql query only when it can’t be done using HQL, such as using the database-specific feature.
  • If you have to sort the collection, use ordered list rather than sorting it using Collection API.
  • Use named queries wisely, keep it at a single place for easy debugging. Use them for commonly used queries only. For entity-specific query, you can keep them in the entity bean itself.
  • For web applications, always try to use JNDI DataSource rather than configuring to create a connection in hibernate.
  • Avoid Many-to-Many relationships, it easily implements using bidirectional One-to-Many and Many-to-One relationships.
  • For collections, try to use Lists, maps and sets. Avoid array because you don’t get benefit of lazy loading.
  • Do not treat exceptions as recoverable, roll back the Transaction and close the Session. If you do not do this, Hibernate cannot guarantee that the in-memory state accurately represents the persistent state.
  • Prefer DAO pattern for exposing the different methods that can be use with entity bean
  • Prefer lazy fetching for associations

So these are some of the most frequently ask Hibernate Interview Questions and answer. Hope this proves helpful to you. Moreover, keep exploring more, to get a better grasp over the concepts and features of Hibernate.

Similar articles – Hibernate Interview Questions and answer

Leave a Comment

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

Scroll to Top