How to setup Hibernate project in Eclipse

So, we started with advantages of Hibernate in my last post. In this post, I will show you (with screenshots) how to setup Hibernate project with Eclipse.

Now, since in most Advanced Java Courses around the world Hibernate is covered after Servlet/JSP web apps, students automatically assume that Hibernate works only with a dynamic web application project. But that is not the case. We can use Hibernate with a normal Java app which begins with a main() method.

In this and our upcoming posts, we will see how to create a Hibernate project which works on the Java console Java application and connect it to a MySQL database using Hibernate. Here’s an outline of posts in this series (live updated):

  1. Setting up Hibernate project in Eclipse (this post)
  2. How to write your own hibernate.cfg.xml file
  3. Using SessionFactory of Hibernate in a Singleton design pattern (with code!)
  4. Basic CRUD operations with Hibernate
  5. How HQL and Criteria Queries can help you perform your operations better?

Before we begin, let me clarify that this method will work with a console-based Java app or a web app, so no need of customization.

To start with, you have to create a simple Java project in your Eclipse for a console-based Java app. If you want to develop a web app with a Servlet, JSP or any other front-end, you can follow the step 1 of this article to create a dynamic web project in Eclipse (You can also create a Servlet as mentioned in that page!).

For the sake of simplicity, I will create a console-based Java app. I don’t like names like “HelloWorld” or “MyFirstHibernate”, so I will simply name my project as “StartingJava”.

Step 1: Add the required JARs to setup Hibernate project

Now that you have created a project as per your need, you have to make sure you have all the necessary JARs included. JARs files are compressed versions of all the packages created in your app; a JAR file actually contains “.class” filed (compressed) for the corresponding projects. This helps you share your project functionality without actually sharing your source code.

Download the latest version of required Hibernate JARs from the official Hibernate website and keep them in an easily identifiable location. These JARs will need to be added for each Hibernate project separately, this is the actual Hibernate installation step in your project. You can import classes and interfaces from the org.hibernate package after you have installed Hibernate JARs.

To add them to your project, follow these steps.

  • Right-click your project and select Build Path
  • From the sub-menu, select Configure Build Path
Build Path Configuration When We Setup Hibernate Project

Build Path Configuration When We Setup Hibernate Project


  • From the resultant dialog box, click the Libraries tab.
  • Then click the Add External JARs button
  • Browse to the location where you stored all the downloaded JARs, select all of them and click Open. You will see them added to your list of libraries.
  • Click Apply and then click Ok

Step 2: Add the JARs to the lib folder of your project

Many times, I have seen a situation while executing a project in Eclipse, where despite installing the required JARs when Hibernate project was created, there would still be a ClassNotFoundException. To solve this, I have added all my JARs again in the projectRoot / lib folder.

Why not avoid that situation altogether and add the files in the projectRoot / lib folder right from the start?

The Hibernate directory structure

A console Java app does not have anything special in its directory structure apart from changes in the lib folder as mentioned above.

Setup Hibernate Project - Post-installation Directory Structure

Hibernate Directory Structure

A web application will have web.xml file in the projectRoot/WebContent/WEB-INF sub folder. All your Java source packages will be in src  folder and if you have JSP pages, they will reside directly under projectRoot/WebContent directory.

Since we are in the training domain, I decided to give my package name as edu.hibernate instead of com.hibernate.

What are Hibernate configuration files?

Hibernate is an ORM (Object Relational Mapping) Framework; it connects to the database and maps your tables to your classes. It does this configuration using at least one xml file.

hibernate.cfg.xml & hbm.xml files

There is still at least one essential xml file to be added in your Hibernate project. Why am I saying “at least” you will come to know by the end of it.

  • hibernate.cfg.xml: This file lists down the configuration parameters such as the database name, driver name, connection string, and few other parameters that are essential for a database connection. You can use any of the methods mentioned in the below paragraph, the list of mapped classes have to be included in hibernate.cfg.xml file.


  • hbm.xml files: The actual ORM mappings i.e. class-to-table, and column-to-field mappings can be specified in two different ways. First is separate hbm.xml files for each mapped class. That means if you have two classes (say, Student and Employee) mapped to two tables in your database, you will have to create your own Student.hbm.xml and Employee.hbm.xml files. An alternate way of specifying these ORM mappings is through Hibernate annotations, which does require you to maintain any hbm.xml files.

Since almost all details in these files are database specific, I will following in a separate post:

  • How to write hibernate.cfg.xml
  • How to write individual hbm.xml files for each class
  • Annotation-based mapping approach

Leave a Reply