Build a Java EE Application with Hibernate Framework : Part 1 — Introduction & Project Setup

Yassine El Baaj
3 min readNov 19, 2019

--

1. Introduction

1.1 What is Hibernate ?

When you are developing a web or a desktop application, it could be cumbersome and time-consuming to work with Java Objects and Relational Databases Tables. This is due to the paradigm mismatch between how data is described in objects versus relational databases. Hibernate is an open source framework that manages the persistence of objects in a relational database. It also offers an Object/Relational Mapping solution for Java environments. Object/Relational Mapping refers to a technique that consists of mapping data from an object model representation to a relational data model representation (and visa versa).

1.2 Hibernate Advantages

  • Hibernate offers numerous APIs that allow the developer to get rid of the repetitive code from the JDBC API.
  • Hibernate code is portable in other ORM frameworks.
  • Hibernate supports inheritance, association, and collections that are not available in JDBC API.

1.3 Hibernate Disadvantages

  • Hibernate is slower than JDBC.
  • A lot of effort is required to learn Hibernate.
  • It is not suitable for small projects.

1.3 The Architecture of Hibernate

Here is a detailed view of Hibernate Application Architecture with its important core classes :

  • The Configuration Object : It is the first object that you create in any Hibernate application. It encompasses miscellaneous properties concerning the configuration of Hibernate such as : The Database Connection and the Class Mapping Setup .
  • The SessionFactory Object : It contains information concerning mapped objects.
  • The Session Object : It allows us to establish a physical connection with the database. Persistent objects are saved and retrieved through a Session Object.
  • The Transaction Object : It is used to perform one single database operation. It is connected with a concrete session and is made by means of :
Transaction transaction = session.beginTransaction( ); 

transaction.commit(); // confirms a transaction and apply changes
transaction.rollback(); // rollbacks or cancels a transaction
  • The Query Object : This object uses SQL or Hibernate Query Language (HQL) to retrieve data from the database and create objects.
  • The Criteria Object : It lets you manipulate objects that contain the properties you would like to retrieve instead of having to step-by-step spell out the components of the query.

2. What Are We Going To Build ?

Library is the name of the website that we will create and evolve over this tutorial. As you would expect, the purpose of the website is to provide an online catalog for a small library, where users can browse available books.

This tutorial tutorial is meant to give you a global understanding of how Hibernate works. We are not going to dive deep into Hibernate’s features.

3. Setting Up The Environment

Tools

  • Eclipse IDE for Java EE.
  • Java Development Kit 1.8 or a superior version.
  • Apache Tomcat 8 or a superior version.
  • MySQL 5 or a superior version.
  • Maven 3 or a superior version.

Note that a good understanding of Java EE development, databases and Maven is required.

4. Creating The Project

  • Open your Eclipse IDE, to to File>Create Dynamic Web Project and enter the name of the project : “Library”.
  • In the “Target runtime” section, select “Choose runtime”. Select the Tomcat version that you just installed, click “Next” and set the Tomcat installation directory in your filesystem. Click on “Finish
  • Click on “Next”.
  • Your project has been created now. Right-Click on it, go to Configure>Convert to Maven Project.
  • A windows will appear, replace the Group Id with “com.library”.
  • Right-Click on your project again, go to Run As>Run on server.
  • The server has been started. We are now ready to start developing our web application.

What is next ?

In the next tutorial, we will add the required dependencies to our project and create the Java Classes ( or models ) that will compose our database.

--

--