Build a Java EE Application with Hibernate Framework : Part 2— Adding The Dependencies and Creating The Database

Yassine El Baaj
3 min readNov 19, 2019

--

In the previous tutorial, we talked about the Hibernate framework and described its architecture. In this tutorial, we are going to add the required dependencies to work with Hibernate. We will also create the different classes or models that will compose our database.

1. Describing A Maven Project

1.1 The Architecture

A Maven Project has a quite similar architecture to the common Java or Java EE projects. However, to make things really clear, I am going to explain the purpose of the main files and directories within your project.

  • JavaResources/src : This is where we will store our packages and our Java Classes.
  • WebContent/WEB-INF : This is where we will store the different .jsp files that will compose our web application.
  • pom.xml : This file acts like the main entry point of your web application, it contains miscellaneous information about it such as : The name, the Maven version, the Group Id… This is also where we are going to add all the dependencies.

1.2 The dependencies

Go to your pom.xml file, before the <build> tag, create a <dependencies> tag that will contain all of the following dependencies :

<dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.8.0.Final</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

2. Configuring Hibernate

Hibernate requires to know in advance where to find the mapping information that defines how your Java Classes relate to the database tables. It also requires a bunch of information regarding the connection with your database. All such information is mentioned in a configuration file named hibernate.cfg.xml.

In your JavaResources/src folder, create a hibernate.cfg.xml file :

The hibernate.cfg.xml file contains different properties :

  • hibernate.dialect : It makes Hibernate generate the corresponding SQL queries regarding the DBMS.
  • hibernate.connection.driver_class : The JDBC Driver Class.
  • hibernate.connection.url : The JDBC URL to the database instance. Note that the database will be created automatically if we add the option “createDatabaseIfNotExist=true
  • hibernate.hbm2ddl.auto : Enable Data Definition Language ( DDL ).
  • hibernate.connection.username and hibernate.connection.password : The identifiers to access your MySQL database.

3. Creating The Database

3.1 The Database Schema

The following diagram show the different relations between the models of our database :

Database Schema

3.2 Creating The Models

In your JavaResources/src folder, create a new package named “com.library.models”. In this package, create 3 new Java Classes : Book, Author and Genre.

Book Class :

Author Class :

Genre Class :

Hibernate annotations are the best way to define mappings between Java Classes and SQL tables.

  • @Entity Annotation : It marks the class as a persistent class.
  • @Table Annotation : It allows you to specify to name of the table that corresponds to your class in the database.
  • @Id Annotation : It allows you to mark a particular field as the primary key of the SQL table.
  • @GeneratedValue Annotation : Used to set the identifier generation policy.
  • @Column Annotation : Allows you to specify which column in your SQL table will be mapped to a particular field within your Java Class.

What is next

We have successfully added the dependencies and created the required models that compose our database. In the next tutorial, we will create our first Java Servlet and start to implement Hibernate features.

--

--