Hibernate Hello World Example in Eclipse (Xml configuration)

Hibernate Hello World Example in Eclipse tutorial will show you how to create simple hibernate CRUD application using MySQL database. The tutorial also demonstrate detailed CRUD operations like saving few records, list all of the persisted records from database, update a record in database and delete a record in database.

 

 

Other interesting posts you may like

Create database Table
The stuff that we need to do before creating the Hibernate Hello World Example in Eclipse is MySQL installation on local computer and create database and table. You can aslo use MySQL Workbench or PhpMyAdmin to work with Mysql.

In this example, we create table by executing the script:

Project directory structure
Our Hibernate Hello World Example in Eclipse will have project directory structure like this:

Hibernate Hello World Example in Eclipse

Maven dependency
We will use hibernate and mysql dependency in project and here is our pom.xml:

Create Model
We create a simple POJO class named Student which be mapped to STUDENT table in database. This table is created in the above.

Don’t forget to override hashcode and equals method. It is a good practice and is required to work with collections or detached instances.

Create Hibernate Mapping File

Hibernate need to know the declaration that our java class and fields is mapped to Table and Columns in database. This declaration will be defined in file Student.hbm.xml:

This file is placed in the directory src/main/resources folder as shown in project structure above.

Base the above file, Hibernate know that class Student is mapped to table STUDENT. Properties name, enteringDate, nationality, code of class Student will be mapped to columns NAME, ENTERING_DATE, NATIONALITY, CODE of table STUDENT. Id property of Student class is mapped to primary key of table Student and using native generator to tell hibernate can choose any of sequence, identity or hilo algorithm for generating the primary key.

Create Hibernate configuration File

We need to provide for hiberate all stuffs like database dialect, driver class, url and account information to connect database. These stuffs is declared in file hibernate.cfg.xml like this:

This file is placed in src/main/resources folder.

Create Hibernate Utility class

For configuring hibernate on startup and managing session factory we create the HibernateUtil class like this:

Create a program to perform CRUD operations on Database

Hibernate works with database by mechanism or steps like this

1. Open session
2. Begin transaction
3. Do something with entities (save/update/delete)
4. Commit transaction

We will also demonstrate these steps in our program. The program will perform CRUD operations like saving few records, list all of the persisted records from database, update a record in database and delete a record in database.

Now we create Hibernate Hello World Example in Eclipse like below.

Run the program and the output should be generated like this:

Hibernate Hello World Example in Eclipse

That’s it on the Hibernate Hello World Example in Eclipse tutorial.

Download complete source code by hitting the below link

HibernateHelloWorldExample.zip (47 downloads)


Leave a Comment

*

Please share it if you found this useful
Hide Buttons