Hibernate Interview Questions


Question 1:

What type of transaction management is supported in Hibernate?

Hibernate can be used in managed(For eg. inside Spring container) as well as unmanaged environments(for eg. Swing Application). Hence, it supports following transactions 

  1. Non Managed Transactions: JDBC
  2. Managed Applications: JTA
Question 2:

What is lazy loading and how do you implement it in hibernate?

Let us take an example of Books section of Modernpathshala website. It comprises of various categories under which there may exists 1000's of books. So the relation will be One to many between Category and Book Table. So while fetching Category either we can get books associated with it. This approach will not only take huge amount of memory but also degrade the performance of the system. To manage this problem, Hibernate provides a facility called lazy loading. When enabled, an entity’s associated entities will be loaded only when they are directly requested.

To enable lazy loading explicitly you must use "fetch = FetchType.LAZY" on a association which you want to lazy load when you are using hibernate annotations as shown in below example:

@OneToMany( mappedBy ="category", fetch = FetchType.LAZY )
private List<Book> bookList;
Question 3:

What are different states of an entity bean in hibernate?

The entity bean can be in three different states in hibernate i.e 

  1. Transient
  2. Persistent and
  3. Detached
Question 4:

What is difference between transient, persistent and detached object in hibernate?

As we know every entity in hibernate can have three states i.e transient, persistent or detached

State of Domain Object

Transient

A newly created instance of a persistent class which is not associated with a Session, has no representation in the database and no identifier value. Such objects in Hibrnate are considered in transient state.

Persistent

An object associated with Session is said to be in persistent state and has a representation in the database , an identifier value. You can make a transient instance persistent by associating it with a Session.

Detached

If the Hibernate Session is closed, the persistent instance associate with session becomes a detached instance.

 

Question 5:

What is n+1 problem in Hibernate?


Question 6:

What is ORM?

Often we need to persist/store our application data. For example:- Registration data for further login, oder information etc. For this, most often we use database to store the data. These databases like Oracle, DB2 are relational database whereas programming language like C#, Java are object oriented. 

Object-Relational Mapping (ORM) is a technique that lets you query, manipulate and access data from database using an object oriented paradigm. This is done by mapping of database tables to domain object classes.

For example:

User

Id Name Age

The above User table will be represented as domain object class as shown below:

class User{
int id;
int age;
String name;
   public void setId( int id ) {
      this.id = id;
   }
   public int getId() {
      return id;;
   }
   public String getName() {
      return name;
   }
   public void setFirstName( String name ) {
      this.name = name;
   }
   public int getAge() {
      return age;
   }
   public void setAge( int age ) {
      this.age = age;
   }
}

All the operations like update, create to User table will be done through User entity class in application.

Question 7:

Explain hibernate architecture?

The below diagram shows a high-level view of Hibernate architecture.

Hibernate Architecture

Hibernate archirtecture has following components:

Configuration: The org.hibernate.cfg.Configuration object reads the configuration files to connect database. SessionFactory object is creaated used Configuration object.

SessionFactory: The SessionFactory is thread-safe as it is created one per database and can be concurrently accessed by multiple threads and is created at the time of application startup. It is created using Configuration object. It is used to create session object.

Session: The org.hibernate.Session is used to create object that are used to communicate between application and persistent store(database). This is factory for org.hibernate.Transaction. As it is not thread-safe, application should create and destroy it once a unit of work done though it may be used to multiple units as per requirement. 

Transaction: The org.hibernate.Transaction is used to represent unit of work. A transaction is associated with a session and is usually instiantiated by a call to Session.beginTransaction()

Query: The org.hibernate.Query is an object oriented representation of Hibernate query. A query instance is obtained by calling session.createQuery(). A query object is used to bind query parameters like number of results fetched etc.

Criteria: The org.hibernate.Criteria is used to retrieve entities by composing some criteria like getting all users by specific age or specific weight etc. The Session is factory for Criteria.

Question 8:

What are the key benifits of Hibernate?
OR,
What are the advantages of Hibernate over JDBC?

Rather we start with advantages of Hibernate, let us discuss about disadvantage of traditional JDBC to communicate with database/persistence store.

  1. In JDBC you have to write huge queries which not only requires understanding of SQL syntax but also make code redundant throughout the application. What if any framework which can result in no sql query code in application and provides in-built methods to interact with database. The answer is ORM.
  2. If you write sql-queries in application and imagine in future if you want to change your database, it will require to change your SQL queries and code. Hibernate is database independent. Hibernate abstracts our application away from SQL queries. If you want to switch to other SQL database it requires few changes in hibernate configuration files rather change in code.
  3. If you want caching, in JDBC you need to implement it, whereas hibernate supports two level of caching. 
Question 9:

What is hibernate session and session factory?

SessionFactory: The SessionFactory is used to configure hibernate for the application and is thread-safe as it is created one per database and can be concurrently accessed by multiple threads and is created at the time of application startup. It is created using Configuration object. It is used to create session object.

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Session: The org.hibernate.Session is used to create object that are used to communicate between application and persistent store(database). This is factory fororg.hibernate.Transaction. As it is not thread-safe, application should create and destroy it once a unit of work done though it may be used to multiple units as per requirement. 

Session session = sessionFactory.getCurrentSession();

Question 10:

How do you configure session factory in spring configuration file?

You can configure hibernate using applicationContext.xml  where you can define datasource  and add SessionFactory bean and finally add TransactionManagerBean.

 

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media