Persistence in Spring

Editor's note: It's usually better to solve problems with simplicity and finesse rather than muscle. That's the premise behind the recently released Better, Faster, Lighter Java. Its authors, Bruce Tate and Justin Gehtland, put this belief to the test in this two-part series on Spring, excerpted from Chapter 8 of their book. This week Bruce and Justin continue where they left off in part one, taking you through adding persistence to the Pet Store example, and looking at the area of presentation logic in the Spring framework.

Adding Persistence

The CartItem object does not necessarily need to be persistent. On the other hand, you'd expect to
pull products and categories from a database. J2EE application developers have long searched for a clean approach to persistence without much success. The best persistence frameworks allow
transparency and do not invade the domain model. Spring lets you
separate your transparent object from the data access layer. Spring
then makes it easy to layer on persistence. You can use a JDBC
abstraction layer, which abstracts away many of the tedious and
error-prone aspects of JDBC, such as connection management and error
handling. The Spring JDBC layer uses a feature called callback templates to pass control from your application to the framework. With this strategy, Spring removes the need to manage
connections, result sets, and RDBMS-specific errors. This framework
is useful when you want to use JDBC directly to process relational queries.

Often, you'd rather deal with objects instead of
relations. Spring also has an appropriate model for transparent
persistence. The jPetStore uses
Spring's OR mapping layer, which provides a variety
of prepackaged choices. Spring now supports mapping layers for basic
JDBC DAO, Hibernate, and JDO. This example uses a DAO framework
called iBATIS SQL Maps to implement a Spring DAO layer.

The Model

Each of the Spring solutions starts with a transparent domain model. Example 8-3 starts with the transparent model
object, a product.

There's nothing special here. It consists purely of properties, accessed through getters and setters, and one utility method, toString. When you look into the jPetStore application, you'll find similar classes for each of the other persistent objects in the domain: Account, Order, Category, Item, and LineItem.

The Mapping

As with Hibernate, the iBATIS SQL Maps framework has a mapping file. In it, each persistent property in your Java bean maps onto a single database column. Using SQL Maps, create all of your SQL within that mapping file as well, isolating all SQL to your XML mapping files. Example 8-4 shows the XML mapping support for Product.

That's simple enough. You can see an interface for
each of the queries defined in the mapping. Specifically, you can see
an interface getProduct that finds a product by
ID, one for getProductListByCategory that returns
all products in a category, and one for the dynamic query based on
keywords. Now, the DAO throws Spring exceptions; any logic that uses
the DAO will have consistent exceptions, even if you later decide to
change implementations.