May 05, 2017

#Part 1: Hibernate Interview Questions

What is an ORM tool?
Object-relational mapping (ORM) is a technique that convert the data between relational databases and object oriented programming languages such as Java, C# etc. An ORM tool helps in simplifying data creation, manipulation, and access. It internally uses the Java API to interact with the databases.

What are the advantages of ORM over JDBC?
  • It lets business code access objects rather than DB tables.
  • Hides details of SQL queries from OO logic.
  • We don't need to deal with the database implementation.
  • Entities based on business concepts rather than database structure.
  • Transaction management and automatic key generation.
  • Fast development of application.
Name some of the ORM frameworks based on JAVA.
Enterprise JavaBeans Entity Beans, Java Data Objects, Castor, TopLink, Spring DAO and Hibernate.

What is Hibernate?
Hibernate is one of the most popular Object-Relational Mapping(ORM) solution for Java. It is a lightweight tool and most importantly open-sourced which gives it an edge over other frameworks. Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieve the developer from 95% of common data persistence related programming tasks.

What are the advantages of using Hibernate?
  • It is open-sourced and lightweight.
  • Minimize database access with smart fetching strategies.
  • Performance of Hibernate is very fast.
  • Helps in generating database independent queries.
  • Provides facilities to automatically create a table.
  • It provides query statistics and database status.
  • Hibernate uses Hibernate Query Language(HQL) which makes it database-independent.
  • It supports auto DDL operations.
  • This Java framework also has an Auto Primary Key Generation support.
  • Supports cache memory.
  • Exception handling is not mandatory in the case of Hibernate.
Suppose you want to store user information in the database. Most of the time, you will be using a relational database. You will be creating a user table, which has the columns corresponding to variables present in your Java class. i.e a class corresponds to a table, and an object of a class corresponds to a row in the table.  We need to write SQL queries, to select, insert, update details in the user table.

We have objects in Java, but not in the database, so we need to take the values of member variables from Java object and map it in the SQL queries. If you add a new column in the table, then you need to make changes in Java classes.

What if an object has a reference to another object, then you need to create tables with foreign keys.

Also, you need to manually do the data-type conversion.

Hibernate solves the above pain points.

Suppose, you want to save the object in the database, without Hibernate, we need to do following things:
  • We need to create the database table.
  • We need to do JDBC configuration.
  • We need to write a model object, which is the object which we need to save.
  • A service method, which will create this model object.
  • A DAO class, which will generate the SQL queries to save the object.
If you are using Hibernate, then you need to:
  • You need to write a hibernate configuration file (hibernate.cfg.xml), which will give information to Hibernate to which database it needs to connect.
  • Create a model object.
  • Write a service method, which will create an instance of a model object and pass it to Hibernate API.
Name some of the databases that hibernate supports.
Hibernate supports almost all the major RDBMS. e.g: MySQL, PostgreSQL, Oracle, Microsoft SQL Server Database etc.

What is HQL?
HQL is the acronym of Hibernate Query Language. It is an Object-Oriented Query Language and is independent of the database.

What is the difference between JPA and Hibernate
JPA defines the specification. It is an API, which tell us how do you define entities? How do you map attributes? How do you map relationships between entities? Who manages the entities? etc. Hibernate is one of the popular implementations of JPA.


-K Himaanshu Shuklaa..

No comments:

Post a Comment