8 Frequently Asked Hibernate Interview Questions.

Hibernate Interview Questions and answers

Hibernate ORM is a java based tool that provides a framework for mapping application domain objects to the relational database tables and vice-versa.

Here in this article, I am going to share 8 Frequently asked basic Hibernate framework interview questions that help you to Crack Java Interviews.

1. What are the main features of Hibernate?

Main features of Hibernate:

The most important Hibernate’s feature is mapping from Java classes to database tables (and from Java data types to SQL data types), but also to provides data query ( and retrieval facilities ). It is important that Hibernate generates the SQL calls and relieves keeping the application portable to all SQL databases, with database portability delivered at very little performance overhead. This feature can significantly reduce development time that programmer would have to spent with manual data handling in SQL and JDBC.

Resuming Hibernate allows creating high-performance database applications with Java much faster and easier. Thanks to transparent persistence for Plain Old Java Objects (POJOs) all you need to do is build a simple POJO, next create XML mapping file that will describe relationship between the database and the class attributes and at the end call Hibernate API’s to specify the operations You can use Hibernate as in standalone Java applications or as in Java EE applications using servlets or EJB session beans.

2. What is object relation mapping in Hibernate?

ORM — Object Relation Mapping. Almost all contemporary databases are relational and based on SQL language. Most programs using them are created in an object way. So some kind of incoherence appears at the meeting point of a database and software. We need some appropriate tools to translate data from relational language into an object language and the other way round. Service of JDBC is extremely troublesome while dealing with more complex applications. ORM deals with the following tasks: Hibernate is currently one of the most popular ORM solutions. Its popularity and effectiveness were proved by the fact that e.g. an implementation of CMP in EJB3 on JBoss server is based on Hibernate.

What is more, a lot of solutions were copied during designing EJB3 specification. Other, common solutions ORM include CMP, JDO. Hibernate, as opposed to them, is not a standard but a concrete solution. Its main advantage is — lightness it is no need to use special containers, it is enough to attach an appropriate library. One can also use it to both: web and client applications. There is no need to generate manually an additional code. As opposed to e.g. JDO, Hibernate requires the only presence of configuration files. Additional classes used by it are generated during performing a task.

3. Which design patterns are used in Hibernate?

Hibernate uses following Design patterns

  • Domain Model Pattern -An object model of the domain that incorporates both behavior and data.
  • Proxy Pattern for lazy loading.
  • Unit of Work (as part of Session object)
  • Factory Pattern in Session Factory
  • Query Object for Criterion API
  • Data Mapper — A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself.
  • Active Record Pattern
  • DAO
  • Object-Relational Mapping(ORM)

4. Explain Hibernate’s Architecture?

Hibernate is based on similar ideas and patterns like other ORM solutions (JDO, CMP).

Basic classes used while working with Hibernate are:

  • SessionFactory,
  • Session,
  • Transaction

SessionFactory- SessionFactory is a class that provides Session objects. It contains information concerning mapped objects a configuration is being loaded during its formation. An application has usually one object of this class it can be managed by e.g. Spring.

One of the most important objects of Hibernate and one of the most troublesome at the beginning. An object of this class represents a unit of work with a database, it usually equals one connection (Connection) with a database.

It represents a unit of work with Hibernate. It equals e.g. PersistenceManager in CMP. All persistent objects are attributed to some definite session and any attempt to modify them or even ( sometimes) access to them results in an exception.

Session- A Session is opened by SessionFactory.newSession() and closed by session.close()

One session should match a logic part of the program an execution of a task. Sometimes ( but not always!) it can be a transaction. While dealing with www applications session-per-request model is very common and easy to implement. Then the session is opened at the beginning of the processing of a claim and closed at the end. The easiest way to do it is by means of the filter. This model can be a bit troublesome if we store some persistent objects in an HTML session ( that is when we use e.g. JSF, Struts)

Transaction- Transaction equals one database transaction. It is connected with a concrete session and is made by means of

Transaction tx = session.beginTransaction( ); 
We can cancel it or confirm by below methods
tx.commit(); // confirms a transaction
tx.rollback(); // rollbacks or cancels a transaction

5. List Data types supported in Hibernate?

Hibernate maps Java data types into databases. Java type can be mapped into one or more column in the database. We can set the Object’s record as primitive types like int, byte or more complex, for example, java.util.Currency.

Data types supported in Hibernate are

  1. Primitive types.
  2. Date and time types.
  3. Binary and large object types.
  4. Other JDK-related types.

6. What are the different ORM levels in Hibernate?

There are basically four different ORM levels in Hibernate:

  1. Pure Relational ORM: At this level, the entire application is designed around the relational model. All the operations are SQL based at this level.
  2. Light Object Mapping: At this level, entity classes are mapped manually to relational tables. A business logic code is hidden from data access code. Applications with less number of entities use this level.
  3. Medium Object Mapping: In this case, an application is designed around an object model. Most of the SQL code is generated at compile time. Associations between objects are supported by the persistence mechanism. The object-oriented expression language is used to specify queries.
  4. Full Object Mapping: This is one of the most sophisticated objects modeling level. It supports composition, inheritance, polymorphism, and persistence. The persistent classes do not inherit any special base class at this level. There are efficient fetching and caching strategies implemented transparently to the application.

7. What is the main difference between spring and hibernate?

Spring and Hibernate are two different things, Spring has several components like Dependency Injection, Spring MVC, Spring ORM, Spring Rest API. So Spring ORM and Hibernate are kind of similar, they both deal with the object relation model, which means they deal with connection java objects to database entities.

Now if you want to build a Rest API using Spring, you can use the Spring Rest API (Here is the getting start guide for Spring Rest Building a RESTful Web Service) for ORM you can either choose Hibernate or Spring ORM framework in either cases there are advantages and disadvantages but major industry goes with using Hibernate (I might be wrong!!) I’ve used both of them Spring ORM is quite useful in simple scenarios and Hibernate in some complex scenarios, whereas in most complex scenarios both of them are not quite useful when you want to write really complex queries. (Both of them provide functionalities two write queries by yourself in the case of complex scenarios).

8. What is @Transient in Hibernate? What is the use of this?

Hibernate supports various object states. One of this object state is transient. In hibernate which is object relation mapping, it will map or create every variable with the class we have defined. For example, if we declare a new variable it will add its entry to the database. But if a variable is marked @Transient then it will not be added to the database. This means it has no persistence representation in that hibernate session. And after an application is closed these transient objects will be destroyed through garbage collection.

class Edufect{
	private int id;
	private String courseName;
	@Transient
	private int courseId;
}

So in this in our database, we can see the entry of id column, courseName but no courseId column. But if we remove transient annotation from courseId then it will be added as an int to the database.

Thanks for reading this article. If you found this article helpful, please share it on your community.