Developing J2EE Applications Using Hibernate Annotations and Spring MVC
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:
- Setting up up your environment.
- The persistence layer.
- The service, or business logic, layer.
- The UI layer.
- 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:
- J2SE5: You will need the J2SE version 5 (Tiger) version for this application
Download URL: http://java.sun.com/j2se/1.5.0/download.jsp
- Tomcat5.x: I've used Tomcat version 5.0.28 for this application. You can obtain this version, or any later version, from the Jakarta site.
Download URL: http://www.axint.net/apache/jakarta/tomcat-5/v5.0.28/bin/jakarta-tomcat-5.0.28.zip
- Ant: Download the Ant build tool (version 1.6.2)
Download URL: http://archive.apache.org/dist/ant/binaries/
- Hibernate annotations: Next, you will need the Hibernate annotation jars; see Figure 1. Hibernate3 implements the EntityManager of JSR-220 (the persistence API). The Hibernate Annotations package contains the EJB3 and Hibernate3 extension annotations and their internal binding to the Hibernate core.
Download URL: http://annotations.hibernate.org
- Spring: Download the Spring framework with the full distributed jar (version 1.2.4 is used in this tutorial).
Download URL: http://prdownloads.sourceforge.net/springframework/spring-framework-1.2.4-with-dependencies.zip?download
Core spring jar files reside under the "spring-framework-1.2.4dist" directory, see Figure 2.
- Hibernate core: You will use the Hibernate version that comes as distributed jars with Spring. You will find them under the 'spring-framework-1.2.1libhibernate' directory.
- JDBC Driver: You will need the JDBC driver to work with your database. In this application, Oracle9i has been used as the backend DB. You can, however, use any DB. You can download the Oracle9i driver used in this application from here:
- Other required jars: Jars, such as jakarta-commons, cglib, j2ee, edcache, log4j, dom4j, jstl, jaxrpc, and so forth, which you will find under the 'spring-framework-1.2.1lib' sub-directory, are needed. See Figure 3.
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:
|Directory for packages|
|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.