Spring 4 Hibernate 4 Integration With DB Connection Pool


Development environment

  1. JDK 1.8 Downloads
  2. Eclipse Mars Download
  3. MySql Installation

The following example shows you a step by step how to implement the integration of Spring 4 & Hibernate 4 with DB Connection Pool. In this example, you don’t need to create table in advance. But test schema (MySql default schema) is requried. The option hbm2ddl.auto option in spring-bean.xml allows you to create table automatically:

validate: is used to validate database schema. This option doesn’t change your DB.

update: create the table if it doesn’t exist or update the existing table.

create: drop table if exists & create the table.

create-drop: drop table if exists & create the table. At the end of the session, drop the table.

Step 1 Create New Maven-based Dynamic Web Project

Step 1.1 Create New Java Project

  • Launch Eclipse IDE;
  • Go to File -> New -> Others… -> Java Project.
  • Click Next.
  • Choose any project name you want then click Finish.

Step 1.2 Convert The Project To Maven Project

  • Right click to the project;
  • select Configure->Convert to Maven Project.
  • Click Finish.
  • Eclipse will generate Project Object Model (pom.xml) file. Open the file by Eclipse text editor then replace its content by the one below:

Note: you can create normal java project and add jar files to your project build path. But it’s easier & faster using maven as maven is integrated with Eclipse IDE; so you don’t need to manually install it. 

Step 2 Create Spring Bean Configuration File

  • Right click to the project or src folder; select New -> Package.
  • Enter info.java.tips.config in Name field.
  • Click Finish.
  • Right click to the package; select New -> Others… Select XML File under XML category.
  • Click Next.
  • Name the file spring-bean.xml.
  • Click Finish.
  • Replace the content by the following:

Step 3 Create Hibernate Mapping Files

We need 2 mapping files in this example. To create hibernate mapping file; follow the steps bellow:

  • Under package info.java.tips.config; select New -> Others… Select XML File under XML category.
  • Click Next.
  • Enter Product.hbm.xml in File name field.
  • Click Finish.
  • Replace the content by the following:

Step 4 Create JDBC Properties File

  • Right click to WEB-INF folder; select New->Folder and name it classes.
  • Right click to the folder; select New-> Other… Select File under General category and click Next.
  • Name it jdbc.properties then click Finish.

Step 5 Create Java Bean Class

  • Copy the codes below to clipboard.
  • Select src folder in your project (left click to the folder).
  • Press CTRL + V.
  • Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.

Step 6 Create Product Manager Class

Create new class with the content as below:

Step 7 Create Test Class

Create new class with the content as below:

Step 8 Clean & Build

Clean & build the project to download & add maven dependencies to the project build path as well as clean old classes file (if any) & force the IDE to recompile java code.

  • Go to Project on menu bar; select Clean…
  • Select Clean all projects (default)
  • Click Ok to proceed clean & build.

Step 9 Run the application

Right click to the class; select Run As -> Java Application.