Advanced Hibernate Interview Questions and Answers

What is Hibernate Framework?

Object-relational mapping or ORM is the programming technique to map application domain model objects to the relational database tables. Hibernate is java based ORM tool that provides framework for mapping application domain objects to the relational database tables and vice versa.

Hibernate provides reference implementation of Java Persistence API, that makes it a great choice as ORM tool with benefits of loose coupling. We can use Hibernate persistence API for CRUD operations.

Hibernate framework provide option to map plain old java objects to traditional database tables with the use of JPA annotations as well as XML based configuration.

Similarly hibernate configurations are flexible and can be done from XML configuration file as well as programmatically.

What is ORM?

ORM is an acronym for Object/Relational mapping. It is a programming strategy to map object with the data stored in the database. It simplifies data creation, data manipulation, and data access.

What’s the usage of Configuration Interface in hibernate?

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.

Explain the advantages of Hibernate?

Some of the advantages of Hibernate are:

  • It provides Simple Querying of data.
  • An application server is not required to operate.
  • The complex associations of objects in the database can be manipulated.
  • Database access is minimized with smart fetching strategies.
  • It manages the mapping of Java classes to database tables without writing any code.
  • Properties of XML file is changed in case of any required change in the database. For more Java Online Course

What are the advantages of using Hibernate over JDBC?

Major advantages of using Hibernate over JDBC are:

  1. Hibernate eliminates a lot of boiler-plate code that comes with JDBC API, the code looks cleaner and readable.
  2. This Java framework supports inheritance, associations, and collections. These features are actually not present in JDBC.
  3. HQL (Hibernate Query Language) is more object-oriented and close to Java. But for JDBC, you need to write native SQL queries.
  4. Hibernate implicitly provides transaction management whereas, in JDBC API, you need to write code for transaction management using commit and rollback.
  5. JDBC throws SQLException that is a checked exception, so you have to write a lot of try-catch block code. Hibernate wraps JDBC exceptions and throw JDBCException or HibernateException which are the unchecked exceptions, so you don’t have to write code to handle it has built-in transaction management which helps in removing the usage of try-catch blocks.

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

This is one of the most frequently asked Hibernate interview questions, I have seen it several times. The key difference between the get() and load() method is that load() will throw an exception if an object with id passed to them is not found, but get() will return null.

Another important difference is that load can return proxy without hitting the database unless required (when you access any attribute other than id) but get() always go to the database, so sometimes using load() can be faster than the get() method. More skills from Java Online Training

Use the load() method, if you know the object exists, and get() method if you are not sure about the object’s existence.

Name some of the databases that hibernate supports.

Hibernate supports almost all the major RDBMS. Following is list of few of the database engines supported by Hibernate.

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

How to create database applications in Java with the use of Hibernate?

Hibernate makes the creation of database applications in Java simple. The steps involved are –

1) First, we have to write the java object.

2) A mapping file (XML) needs to be created that shows the relationship between class attributes and the database.

3) Lastly,  Hibernate APIs have to be deployed in order to store the persistent objects.

What is the purpose of Session.beginTransaction()?

Hibernate keeps a log of every data exchange with the help of a transaction. Thereon, in case a new exchange of date is about to get initiated, the function Session.beginTransaction is executed in order to begin the transaction.

What role does the SessionFactory interface play in Hibernate?

The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole application created during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work
SessionFactory sessionFactory = configuration.buildSessionFactory();

What is the general flow of Hibernate communication with RDBMS?

The general flow of Hibernate communication with RDBMS is :

  • Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files
  • Create session factory from configuration object
  • Get one session from this session factory
  • Create HQL Query
  • Execute query to get list containing Java objects

What is Hibernate Query Language (HQL)?

Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate query Language (HQL), is an object-oriented extension to SQL.

To get in-depth knowledge, live free demo on Java Certification Course

Leave a comment

Design a site like this with WordPress.com
Get started