Hibernate Interview Questions

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Q1. What's Hibernate Framework and benefits of it?

Ans. Hibernate is an Object-relational mapping (ORM) tool and persistence framework that allows you to map plain old Java objects to relational database tables using configuration files. Below are the benefits of Hibernate Framework.

  • Hibernate is Database Independent framework.
  • Hibernate framework generates SQL statements automatically and save all the unnecessary work that is done in a JDBC, so you can focus on business logic.
  • Hibernate provides a powerful query language (HQL) that is similar to SQL. However, HQL is fully object-oriented and understands concepts like inheritance, polymorphism and association.
  • Hibernate framework provides support for XML and JPA annotations, that makes your code implementation independent.
  • Hibernate is a layered architecture and you can use the components as per the needs of your application.
  • Hibernate framework is easy to integrate with other J2EE frameworks, and also Spring Framework provides built-in support for integrating hibernate with Spring applications.
  • Hibernate supports lazy initialization.
  • Hibernate provides cache and helps you in getting better performance.
  • Hibernate provide option for you to create database tables too.

Q2. What is ORM?

Ans. ORM stands for object-relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.

ORM solution consists of the following four pieces.

  • An API for performing basic CRUD operations on objects of persistent classes.
  • An API to specify queries that refer to the classes and properties of classes.
  • It provides configurable facility for specifying mapping metadata.
  • It provides Optimization facilities like dirty checking, lazy associations fetching

Q3. How properties of a class are mapped to the columns of a database table in Hibernate?

Ans. Hibernate mapping files are used to specify how your objects relate to database tables. Hibernate needs this mapping placing object properties into column`s of a table. Mapping can be given to an ORM tool either in the form of an XML or in the form of the annotations. The mapping file contains mapping from a pojo class name to a table name and pojo class variable names to table column names.

Actually mapping can be done using 2 ways,

  • XML
  • Annotations

Example: The Employee.hbm.xml mapping file

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE"> 
   <id name="id" type="int" column="id">
         <generator class="native"/>
   </id>
   <property name="firstName" column="first_name" type="string"/>
   <property name="lastName" column="last_name" type="string"/>  
   </class>
</hibernate-mapping>
  • The mapping document having hibernate-mapping as the root element which contains all the class elements.
  • The class elements are used to define specific mappings from a Java classes to the database tables. Class name is specified using the name attribute and the database table name is specified using the table attribute.
  • The id element maps the unique ID attribute in class to the primary key of the database table. The name attribute of the id element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute will convert from Java data type to SQL data type.
  • The generator element is used to automatically generate the primary key values.
  • The property element is used to map a Java class property to a column name in the database table. The type attribute holds the hibernate mapping type and it will convert from Java data type to SQL data type.

Q4. What are the Core interfaces of Hibernate framework?

Ans. There are five core interfaces which is used in every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and can control transactions.

  • Session interface
  • SessionFactory interface
  • Configuration interface
  • Transaction interface
  • Query and Criteria interfaces

Q5. What is the usage of the SessionFactory interface in Hibernate?

Ans. Using the SessionFactory, application obtains Session instances from it. There is a single SessionFactory for the whole application created during application initialization. SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. SessionFactory holds cached data that has been read in one unit of work and may be reused in a future.

You can create SessionFactory object using Configuration interface.

SessionFactory sessionFactory = new Configuration().configure().buildSessionfactory();
      Advertisements