Align IT and Business in an Agile Manner

Agile Software Development

Subscribe to Agile Software Development: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Agile Software Development: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Agile Development Authors: Pat Romanski, Jason Bloomberg, Elizabeth White, Charles Araujo, Liz McMillan

Related Topics: Apache Web Server Journal, Scrum Software Development, Agile Software Development, Java Developer Magazine

Apache Web Server: Article

JDJ Cover Story — Agile Java Development with Spring, Hibernate, & Eclipse

A roadmap for building enterprise-class applications using agile methods a POJOs

Agile Java Development
Now that we've discussed agile processes and modeling, we are ready to begin coding. However, before coding can begin, getting the environment set up is important, so let's look at that first before discussing Hibernate, Spring, and other technologies.

Environment Setup: JDK, Ant, JUnit, and Version Control
Before Java application development can begin, some minimal tools are required such as the Java Standard Edition Development Kit, a build tool like Apache Ant, and an important tool for agile development, a unit-testing framework such as JUnit.

Apache Ant
Ant is commonly used to build Java applications, however, it's much more than a build tool. For example, some commonly found Ant tasks include javac, copy, delete, move, junit, cvs, ftp, mail, exec, and sleep - these can be used for everything from file management to code compilation to e-mailing. You can even write your own custom tags and it should be no surprise that there are many Open Source and commercial Ant tasks available.

For details on Ant, visit the ant.apache.org.

JUnit and Test Driven Development (TDD)
Erich Gamma (Gang of Four, Design Patterns book) and Kent Beck originally wrote JUnit. JUnit classes provide various assert methods (for example, assertTrue and assertNull) that let you test the expected results. JUnit is a simple framework but a powerful unit-testing tool. When combined with Test-Driven Development (TDD; testdriven.com), a method created by Kent Beck, it can significantly help you write better, cleaner, stabler code. TDD recommends that you write your test code before writing your actual code - this is a fundamental mind shift but one I recommend you investigate further.

For details on JUnit, visit the junit.org.

Version Control, Naming Standards and More
Finally, getting a development directory structure established, class/file naming conventions defined, and version control software in place such as Concurrent Versions System (CVS) are crucial steps that a development team should take to be highly effective.

Developing Our Data Tier with Hibernate
Relational databases and object-oriented technologies have been with us for a while now and it appears they are here to stay for the foreseeable future. Given the fact that Java developers typically work with both technologies, JDBC is often used to write the mapping code in data access objects (DAOs) that can be used to fetch and save the data - passing data back and forth is typically handled using data transfer objects (DTOs). An alternate approach to using JDBC is to use EJB entity beans, however, before EJB 3.0, they would have been considered heavy-handed since they were remote objects. So, what do you do if you want an agile approach to Java persistence using POJOs? One answer is an ORM framework such as Hibernate.

Object-relational mapping (ORM) code eliminates the need for writing JDBC calls by hand, lets you do the mapping in XML files and then simply work with database records as POJOs. Hibernate is a popular ORM framework widely used in the Java community. In fact, due to its popularity, the EJB 3.0 specification follows the Hibernate model very closely.

Hibernate supports a dozen or so relational databases (through its dialect classes). To use it, we first need to configure the database connection, typically in a file called hibernate.cfg.xml. Then, for each table to be used, we'd typically map the table to a Java class. For example, the following line shows a sample mapping for a table called Department (in a file called Department.hbm.xml) that maps to a Java class called Department as well.

<class name="com.visualpatterns.timex.model.Department" table="Department">

This next line shows mappings for a couple of the columns, with departmentCode being a primary key in database terms and a unique object identifier in Hibernate/ORM terms:

<id name="departmentCode" column="departmentCode">
<property name="name" column="name"/>

Once the database and mappings have been properly configured, we can simply obtain a Hibernate Session (essentially a JDBC database connection) from a SessionFactory and work with the record as a Java object, as demonstrated in the code below, which fetches a Department record with a departmentCode of "IT":

Session session = sessionFactory.getCurrentSession();
department = (Department) session.get(Department.class, "IT");

As you might guess, Hibernate also provides methods to save and delete database records (objects). Some of these methods include save, load, get, update, merge, saveOrUpdate, and delete.

One of Hibernate's most powerful features is its Hibernate Query Language (HQL). This is a SQL-like language and is extremely robust since it supports such things as joins, aggregate functions, parameter substitution, expressions, and sorting. The extremely simple example below demonstrates how we can fetch a java.util.List of objects from the Department table:

departmentList = session.createQuery("from Department ORDER BY name").list();

We've merely scratched the surface here since Hibernate provides many more features; some of them include record locking, associations, native queries, stored procedure support, scrollable iterations, interceptors, and filters.

Visit hibernate.org for more details.

Developing Our Web Tier with Spring
Spring (springframework.org) is one of those framework that is almost impossible to describe in one short sentence because it does so much. For example, it supports IoC or inversion of control (martinfowler.com/articles/injection.html), a complete Web MVC framework, JDBC, ORM, JEE/Web Services, aspect-oriented programming (AOP), declarative transaction management, job scheduling, mail, and more.

Using Spring provides several benefits like easier and cleaner unit testing, the ability to use POJOs in lightweight containers (say, Apache Tomcat) with enterprise services such as declarative transaction management, convenient data access, and consistent data exception handling through ORM and JDBC integration, and job scheduling in a Web/application server.

The Spring Web MVC Framework
The Spring Web MVC Framework (or simply Spring MVC) is a robust, flexible, well-designed framework for rapidly developing Web applications using the MVC design pattern. The benefits to using this Spring module are similar to those you get from the rest of the Spring Framework; however, one additional and very key benefit is the ability to bind directly to business objects unlike other frameworks that require you to extend special sub-classes. Let's review some Java and configuration-related concepts for this framework.

Spring MVC Java Concepts
The key Java concepts in Spring MVC are:

  • Controller
  • Model and view object
  • Command (form-backing) object
  • Validator object
  • Tag libraries
One of the good features of Spring MVC is that it provides a large number of controller classes to choose from (see Figure 8). Of course, this can be a bad thing when you're trying to learn this framework because deciding which to use can be a minor challenge. For example, I tend to use SimpleFormController for HTML forms and UrlFilenameViewController when I don't need a controller. In some cases, I simply implement the Controller interface when I want a no-forms controller.

Many of the key GET- and POST-related Spring controller methods return a ModelAndView object that can contain model-related data and the name of a view (or reference to a view object). For controller classes that support HTML forms, we can have optional command and validator objects to bind the HTML form fields to Java objects and validate the input data, respectively. As for the view itself, Spring supports a variety of view technologies including JSP, Velocity, and JasperReports. Let's look at how we might use JSP for our views.


More Stories By Anil Hemrajani

Anil Hemrajani is the author of the book titled Agile Java Development with Spring, Hibernate and Eclipse. He is the founder of Isavix Corporation (now InScope Solutions, Inc.), a successful IT services company, and DeveloperHub.com (formerly isavix.net), an award-winning online developer community that grew to over 100,000 registered members. He has twenty years of experience in the Information Technology community working with several Fortune 100 companies and also smaller organizations. He has published numerous articles in well known trade journals, presented around the world, and received and/or nominated for several awards. Anil can be reached via his web site, VisualPatterns.com.

Comments (3) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
JDJAgile 07/27/06 07:33:07 AM EDT

Hey Jeff, JDJ used all of the lastest Agile methods to develop their pages, so you see it's fast, it's now, it works! Far be it from me to stand in the way of progress. Give me a break, none of this crap has infiltrated coporate computing in the least. Just go back and code like the rest of us and stop patting each other on the back for your supposed genius.

I'm sure the author is well-meaning but when we're bombarded daily with titles like "The Four Quantum States of Ajax" I just gotta laugh. Do you really think all this stuff is changing anything?!

The engineering world wants real engineering not something built on top of existing frameworks decades old that works and works well.

Jeff 07/25/06 10:48:49 PM EDT

Holy crud, the pop up ad blocks the entire story even at 1360 x 768. Do you really need an Ad that big that requires a click to close? There are already adds on both margins and the in the header and Footer, the content is a thin ribbon down the center that get blocked by yet another ad. Just seems silly to me.