WHERE AND WHEN TO USE THE DATA ACCESS PAT TERN
Some have argued that the DAO is no longer a useful pattern because you can easily invoke the
EntityManager directly. This is a reasonable argument because the EntityManager provides a clean
API that abstracts away the underlying data access layer. It is also reasonable to suggest that the
likelihood of a change in the data provider is remote, which makes the abstraction that the DAO
provides less purposeful. Although these arguments have merit, it is still arguable that the DAO
has its place in a well‐designed Java EE application. (The place might not be where it was originally
The value of extending the BaseDAO as shown in Listing 12‐7 for each entity type is in the extensibil-
ity of each implementation. Methods that are specii c to an entity can be written while maintaining
a common interface. You select the DAO implementation once for each entity rather than choosing
the right EntityManager method each time it is required to persist or retrieve data.
Named queries can be located within the entity to which they relate. This keeps the queries in a logi-
cal place, making maintenance easier. The DAO allows for a uniformed and controlled data access
strategy because all access to the entity's data is required to go via the entity's DAO. It maintains the
principle of single responsibility because only the DAO accesses and manipulates the application's
Don't forget that even though it's remote, the data source might change. If it does, you'll be glad that
there is an abstraction on the data layer.
The DAO has its fans and detractors. The decision of whether to use the pattern in your application
should be based on the design requirements of the application. Like all patterns, its use for use's sake
is potentially dangerous and could cause more confusion because over abstract obscures the purpose
of the code. Ensure that you understand well the various implementations of the pattern and how it
interacts with the DTO and factory pattern.
Write an interface and its implementation for a movie order DAO. You can use the examples in
the text as a starting block.
Write a service façade and a DTO that uses the MovieDAO.
1. Core J2EE Patterns: Best Practices and Design Strategies (Prentice Hall/Sun Microsystems
Press, 2003): Deepak Alur, John Crupi, and Dan Malks.
2. The Java EE 7 Tutorial: Part VIII Persistence .