How to start development with Hibernate – XML Mapping File Hibernate

Hibernate is an Java Object-Relational Mapping framework to map classes’ objects and database tables’s records. Hibernate provides API to save and select database’s records by Java objects. In the tutorial, JavaSampleApproach will show you way to start Hibernate by Java sample code.

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Hibernate: 5.2.10.Final
– MySql database

II. Hibernate

1. Hibernate Configuration

Hibernate requires a configuration file which has 2 parts:
– Database settings are used to establish database connections.
– Mapping files are used to map between Java persistent classes & database tables.


1.1 Database settings

Some important properties for database setting:
hibernate.connection.url: JDBC url instance
hibernate.connection.driver_class: JDBC driver class
hibernate.connection.username: database username
hibernate.connection.password: database password
hibernate.dialect: used to generate the appropriate SQL

1.2 Mapping file

Mapping files are used to map between Java persistent classes & database tables. They are defined in mapping tag:

What is Hibernate persistent class? -> They are POJO Java classes whose objects will be stored in database tables.

Some requirement for Hibernate persistent classes:
– Must have a default constructor.
– Should have an ID to map the objects and database records.
– All persistent attributes have getXXX and setXXX methods.

– Having a class

– The mapping file Customer.hbm.xml with Customer class:

2. Hibernate Core Classes

Configuration Object: represents a configuration file – hibernate.cfg.xml

SessionFactory Object: is used to inturn configures Hibernate (Configuration Object) for the application.

***Note:The SessionFactory is a thread safe & heavy object. Need one SessionFactory object per database.

Session Object: is created via SessionFactory Object and used to get a physical database connection. It is lightweight & not thread safe. Session Object should be created when interacting with database then close it.

Transaction Object: is a database unit work. It is created and associated with a Session Object.

III. Practice – XML Mapping File Hibernate

Create a Simple Spring Maven Application, that uses Hibernate to store and retrieve Customer objects from MySQL database.

XML Mapping File Hibernate

Step to do
– Create Spring Maven project
– Create persistent class
– Setup Hibernate configuration
– Implement Hibernate Application
– Run & Check Results

1. Create Spring Maven project

Using Spring Tool Suite, create a Simple Spring Maven project.
Modified pom.xml file with Hibernate & MySql dependencies:

2. Create persistent class

Create a

3. Setup Hibernate configuration

Under /src/main/resources:
– Create Hibernate Configuration file – hibernate.cfg.xml:

– Create a mapping file for Customer class – Customer.hbm.xml:

4. Implement Hibernate Application

– Implement 6 functions:
setUpSessionFactory(): setup Hibernate Session Factory
saveCustomers(): save Customers to Database by Hibernate
showAllCustomers(): retrieve all customers from MySQL and show all on console.
updateCustomer(int customerId, int newAge): update Customer’s age.
deleteCustomer(int customerId): delete a customer
shutdown(): close Hiberbate Session Factory Object

5. Run & Check Results

– Create a MySQL Customer table by script:

– Run the main class, Logs:

– Database records:
xml hibernate

IV. Sourcecode


By grokonez | April 24, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *