Providing a Complete Data Persistence Solution - A Mapping Architectural Overview
(Page 2 of 4 )
Mapping data requires using a series of primary classes to map data. Let’s put together a Java Mapper series of interfaces that encapsulate common mapping behavior required to create, update, delete, and read from a relational database. The interfaces presented here conform to the Model, View, and Controller (MVC) design pattern so widely adopted in most distributed enterprise applications today. Note that the Java Mapper interface is independent from both the domain model and persistence technologies, as should be the case with all data mappings. The JDBCMapper interface represents an abstract implementation of the JDBC interface.
Examples
Let’s extract some methods from the JDBC Mapper Interfaces and see how they work.
Below are two examples demonstrating how you could implement a Mapper method.
public void update (DomainObject domainObject)
throws NoSuchObjectException {
Connection cn = null;
try {
cn = getConnection();
updateImpl(cn,domainObject);
}catch (Exception e) {
System.out.println(“Exception “ + e + “caught in update()”);
throw new NoSuchObjectException(“Wrapped
Exception“ + e + “caught in update()”);
}finally { close(cn);
}
}
protected abstract void updateImpl(Connection cn, DomainObject
anObject;
throws SQLException, MappingException;
We have presented two methods, public void update(), and protected abstract void updateImpl(). The first method implements the JDBC-specific interface by managing connection-management and JDBC exception handling. The second method calls the abstract method updateImpl, a representation of the domain model object-related behavior. The domain object-specific mapper must implement the abstract Impl methods in order to create, update, delete, and read instances of the object in the relational database. You must call the abstract activate method in order for it to be implemented by domain object-specific subclasses. The RegistrantMapper doActivate() and passivate() methods execute the mappings to and from the results, reading and/or updating the relational database.
Here are examples of both of the doActivate and passivate methods.
protected void doActivate(DomainObject <insert
variable here>,ResultSet rs)
throws SQLException, MappingException {
Registrant r = (Registrant)domainObject;
r.setName(rs.getString(2));
r.setAddress(new address (r));
r.getAddress().setStreet (rs.getString(3));
r.getAddress().setCity(rs.getString(4));
…
}
protected void passivate (DomainObject <insert
variable here>, PreparedStatement ps)
throws SqlException, Mapping Exception {
Registrant r = (Registrant) domainObject;
ps.setString(1, r.getId());
ps.setString(2.r.getName());
if (r.getAddress() != null){
ps.setString(3, r.getAddress()
.setStreet());
ps.setString(4, r.getAddress().getCity());
}
}
Also notice how the RegistrantMapper interface provides the necessary SQL statements to execute the abstract Impl() methods such as read, create, update, and delete. For example, the createImpl method creates a database row and maps the registrant department attributes to the columns in the expression that match the primary key of the table.
The other mapper interfaces, including MapperRegistry, DomainObject, and UnitOfWork, are interfaces provided by Martin Fowler in his Patterns of Enterprise Application Architecture, published by Addison-Wesley. The MapperRegistry interface provides access to specific mappers being used for each unique object residing in the domain model. The UnitOfWork interface maintains a list of objects affected by a business transaction and coordinates the writing of changes.
Figure 1 JDBC interfaces that facilitate mapping from Java to a relational database.

Next: Mapping Tool Types >>
More .NET Articles
More By Dwight Peltzer