Interview Questions and Answers on Java EE – Hibernate Framework (Part -2)

What do you know about the Hibernate proxy and how does it help in lazy loading?

Hibernate uses a proxy object to support deferred loading. Usually, when loading data from a table, Hibernate does not load all the mapped objects. Once you refer to a child object or look for an object with a getter, if the associated entity is not in the session cache, the proxy code will go to the database to load the associated entity. To do this, javassist is used to efficiently and dynamically create subclass implementations for your entity objects.

How are relations implemented in Hibernate?

You can implement a one-to-one, one-to-many, many-to-many relationship using JPA annotations or configuring an XML file. For examples, visit the Hibernate section.

How to manage transactions with Hibernate?

Hibernate generally does not allow most transactions without using transactions. Therefore, after receiving the session instance from the SessionFactory, you must execute beginTransaction () to start the transaction. The method will return a link that we can use to confirm or roll back the transaction.

In general, the management of transactions in the framework is much better than in JDBC. We do not have to rely on an exception to rollback the transaction. Any exception will automatically cause a rollback.

How to integrate Hibernate and Spring?

It’s best to read about the settings on the framework sites for the current version. Both frameworks support integration out of the box and in general the adjustment of their interaction is not difficult. The general steps are as follows.

Add dependencies for hibernate-entitymanager, hibernate-core and spring-orm.

Create model classes and pass the implementation of the DAO operations to the database. It is vital that DAO classes use SessionFactory, which is implemented in the Spring Bin configuration.

Configure the Spring configuration file (see off-line documentation or from the example on this site).

Additionally, it becomes possible to use the @Transactional annotation and stop worrying about managing the Hibernate transaction.

An example of using  Spring Data JPA is an example of the Hello World application. Settings Spring Data + JPA + Hibernate + MySQL.

What do you know about the HibernateTemplate class?

The Spring Framework provides various approaches for integrating with Hibernate. Nevertheless, we will most often use the approach using HibernateTemplate. There are two main reasons:

  • The class hides the details of managing sessions and transactions.
  • Provides a template-based approach

The HibernateTemplate class hides the difficulties of managing sessions and transactions when using Hibernate to access data. You only need to initialize the HibernateTemplate by passing a SessionFactory instance. The Spring Framework takes care of details related to sessions and transactions. This helps to eliminate the infrastructure code, which can make a mess when increasing complexity.

HibernateTemplate, as well as JdbcTemplate, provides a template approach for accessing data. When you use HibernateTemplate, you will work with callbacks. Callbacks are the only mechanism in the template approach that notifies the template to run the desired task. The advantage of having a callback is that there is only one entry point to the data access layer. And this entry point is defined by the template, in this case, HibernateTemplate.

The comments added that the use of HibernateTemplate is not recommended. Instead of using the HibernateTemplate from the org.springframework.orm package, it is recommended that you use a declarative approach (@Transactional). Thus, the framework itself takes care of the operations open, commit, close, flush.


About The Author

Ruchi Singla
An enthusiast writer with years of experience, who loves to create beautiful thoughts into words. She writes with a zeal and enjoys reading vogue with a good cup of coffee when not writing. Ruchi is working as a content writer for many businesses, helping them gain an extra edge over their competitors. As a content writer, she is responsible for enhancing engagement and traffic on the blog by brainstorming exciting content ideas.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.