October 17, 2019
Hot Topics:

Developing J2EE Applications Using Hibernate Annotations and Spring MVC

  • January 12, 2006
  • By M. M. Islam Chisty
  • Send Email »
  • More Articles »

The first part of this three-part series introduced you to the latest JDK5 annotations feature. In this part, you will learn how to apply these annotations to develop real-world applications. With the combination of Hibernate annotation and Spring MVC, developers can drastically reduce the time at the configuration level. No more artless mapping files, no more klutzy XDoclet tags; life becomes so easy! This article describes a step-by-step procedure of how to build, deploy, and run a simple J2EE-based Web application using the above-mentioned technologies.


It is assumed that readers of this article are quite familiar with J2EE technologies including JDBC, containers/servers, ORM tools, and frameworks. It is also recommended that readers go through that first article, "An Introduction to Java Annotations," as a primer.

In J2EE-based Web applications, a traditional approach is to use XML mapping files with Hibernate when defining persistent objects (as in using one hbm.xml file per POJO). For example, if you have 40 POJOs with complex inter-relationships, you will have to define 40 hbm.xml mapping files, which is not only time-consuming but also very error prone. If you use Hibernate annotations, however, there will be no need to use such mapping files anymore. The compiler will read the annotated codes before runtime and process them as accordingly before the class is loaded (the ClassData model).

The annotated beans can be accessed from the Hibernate DAO layer. For Hibernate, Spring provides good support with IoC features, addressing many typical Hibernate issues. Thus, DAOs can be configured through Dependency Injection and participate in Spring's resource and transaction management. The main advantage of the DAO layer is that it completely decouples business logic from persistence technology. The business logic layer (also known as the service layer or the manager layer) can be configured to communicate with the UI layer through some MVC type framework.

MVC is a pattern that helps separate presentation from business logic. Although dependency injection is the basis of the Spring framework, it also provides a rich set of tools built on top of its core dependency injection functionality. It provides an MVC framework, transaction management, support for several O/R mapping tools, and more. I've chosen Spring's MVC pattern for this tutorial to provide you with an idea about how Spring MVC manages a clean division between controllers, JavaBean models, and views.

Rather than providing theoretical descriptions, I will take the examples-with-explanation approach. You will be taken through the following five steps:

  1. Setting up up your environment.
  2. The persistence layer.
  3. The service, or business logic, layer.
  4. The UI layer.
  5. Building, deploying, and running the application.

Step 1: Setup up Your Development Environment

1. Obtain the required jars.

You will need JDK5, Tomcat 5.x, Ant Tool, Hibernate annotations, Spring core, Hibernate 3, plus other commonly required jars to deploy this application. Here is the list and the suggested URLs where you can obtain these:

2. Set up your environment.

Set up an environment for JAVA_HOME, ANT_HOME, and CATALINA_HOME, as demonstrated accordingly.

Note: If you need more instruction on how to set up your development environment, here is a good link you can follow: http://hackydev.ics.hawaii.edu/hackyDevSite/hackyBuild/README.1.html.

3. Create a directory structure.

This is shown in Figure 4:

Root directory: springapp
Source directory: springappsrc
Directory for packages
  • srcorgannotationmvcvo (persistence beans will reside here)
  • srcorgannotationmvcdao (DAO objects here)
  • srcorgannotationmvcservice (Service objects here)
  • srcorgannotationmvcweb (web controller objects here)
Directory for web pages springappwarjsp (JSP pages here)
Directory for configuration files warWEB-INF (xml configuration files here)
Directory for jars warWEB-INFlib (all the required jar files will reside here)

4. Copy and paste.

Copy all the required jar files as mentioned above in Step #1 and paste them under the 'warWEB-INFlib' directory. Figure 5 shows my environment with all the jars that are required for this application.

5. Get the build properties.

Click here to get the build.properties and build.xml files and place these under the root directory "springapp". Edit the build.properties file according to your Tomcat environment. Modify the Tomcat location, url, manager id, and password as needed.

Page 1 of 4

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date