| This Chapter | |
| - | Chapter 15: The Persistence Layer |
| - | The DAO Pattern |
| - | Implementing the DAO Pattern |
| - | Complex Data Structure |
| - | Hibernate |
| - | Summary |
At some stage, application data needs to be persisted or saved to secondary storage. In Struts, application data is encapsulated in Java objects. For example, user input is used to populate an action form (which is a Java object) upon form submission, business methods accept transfer objects as arguments, and custom tags are designed to manipulate JavaBeans.
The challenge of storing data in Struts applications, therefore, is to effectively persist Java objects. Several methods are available, including storing them into files, relational databases, XML documents, and so on. Of these, persisting data to a relational database is still the most reliable and the most popular. In addition, object-to-relational database mapping tools can be purchased off the shelf to help Java programmers persist their objects.
Without a mapping tool, you have other options in hand. These include the Data Access Object (DAO) pattern, Java Data Objects (JDO), and the open source Hibernate project. Of these, the DAO pattern in the easiest to learn and is sufficient in most applications. This chapter will show you how to implement the DAO pattern for data persistence.
Also note that because many parts of an application may need to persist objects, a good design dictates that you create a dedicated layer for data persistence. This persistence layer provides methods that can be called by any component that needs to persist objects. In addition to simplifying your application architecture (because now object persistence is handled by only one component), the persistence layer also hides the complexity of accessing the relational database. The persistence layer is depicted in Figure 15.1.
Figure 15.1: The persistence layer
The persistence layer provides public methods for storing, retrieving, and manipulating value objects, and the client of the persistence layer does not have to know how the persistence layer accomplishes this. All they care is their data is safe and retrievable.