Top 20 Hibernate Interview Questions

Hibernate is most popular ORM tool in Java.If you are fresher or experienced ,this article is useful for you.In this article I covered all Top 20 Hibernate Interview Questions and answers.

Top 20 Hibernate Interview Questions and Answers:-

What is Hibernate?

Hibernate is most popular ORM(Object Relational Mapping) tool in Java.It is developed by Red Hat.It is open source and light weight tool.It is used in enterprise application for database operations.It is used for store and retrieve data from database.

What is HQL in Hibernate?

HQL stands for Hibernate Query Language.It is independent of the database.

Explain Hibernate architecture?

Hibernate is good for those who is not aware about SQL.Hibernate use objects to develop persistence logic which are independence of database.Let’s discuss architecture.

What are core interfaces of Hibernate?

The core interfaces of Hibernate framework are given below:-

  • Sessionfactory

  • Session

  • Configuration

  • Transaction

  • Query and Criteria

What is ORM?

ORM stands for Object Relational Mapping.It is used for map the object with data stored in database.

What are advantages of Hibernate?

It create automatic tables in database.

It is open source and light weight tool.

Hibernate performance is very fast.

It provide HQL to querying data.

How to achieve mapping in Hibernate?

Association mapping is most popular feature of Hibernate.

One to One Association

Many to One Association

Many to Many Association

What is session in Hibernate?

It is single threaded short lived object representing a conversation between application and persistent store.Application obtains session interface from session factory for whole application created during application initialization.

Session sess=sessionfactory.openSession();

What is sessionfactory in Hibernate?

Session factory cache generate sql statement and other mapping metadata that hibernate uses at runtime.

There is typically single session factory for whole application.

The session factory interface present in org.hibernate package.

Session factory interface is immutable and thread safe.

SessionFactory sfactory=conf.buildsessionFactory();

What  is transaction in Hibernate?

This interface abstract the code from any kind of transaction.

This interface is used for data consistency.

Transaction interface use commit() method to make permanent changes in database.

Transaction tx=session.beginTransaction();

Tx.commit();

What is Query and Criteria interface in Hibernate?

This interface allows user to perform queries and also control flow of query execution.

Hibernate provide different technique to query database such as Named Query and Criteria API.

It is present in org.hibernate package.

Query query=session.createQuery();

Criteria criteria=session.createCriteria();

What are databases that hibernate support?

  • PostgreSQL

  • MySQL

  • Oracle

  • DB2

  • Microsoft SQL Server Database

  • Sybase SQL Server

  • HSQL database

  • Informix Dynamic Server

  • FrontBase

How is SQL query created in Hibernate?

SQL query created with below syntax:

Session.createSQLQuery

What is persistence class?

Classes whose object stored in database is called as persistence class.

Does session a  thread safe object?

No,session is not thread safe object.Many threads access session object simultaneously.

What is difference between session.save and session.persist method?

session.save():-

Returns generated id after saving.

Its return type is serializable.

Its for detached object will create new row in the table.

We can invoke this method outside of the transaction.

session.persist():-

It does not return generated id after saving.

Its return type is void.

It does not save changes to database outside of the transaction.

What is difference between get and load method?

Get:-

It returns real object.

Get() method always hit the database.

It returns null object if object is not found.

It is slower than load method.

If you are not sure about object exist then use get method.

Load:-

It return proxy object.

Load method doesn’t hit the database.

It is faster than get method.

It should be used if you are sure that instance exist.

What are the states of the object in Hibernate?

There are three states of object in Hibernate.

Transient:-

Object is created but not associated with any session is called transient object.

Persistent:-

An object is associated with session is called persistent object.

Detached:-

Object which is just removed from session is called detached object.

What are collection types of Hibernate?

There are five types of collection.

  • Bag
  • Set
  • List
  • Array
  • Map

What are inheritance mapping strategies?

There are three inheritance mapping strategies.

  • Table per class hierarchy

  • Table per concrete class

  • Table per subclass hierarchy

 

 

Leave a Reply

%d bloggers like this: