org.dartra.standard.dao.jdbc
Class BaseDAO

java.lang.Object
  extended byorg.dartra.standard.dao.jdbc.BaseDAO
All Implemented Interfaces:
org.dartra.framework.dao.DataAccessObject
Direct Known Subclasses:
HSQLDBBaseDAO

public abstract class BaseDAO
extends java.lang.Object
implements org.dartra.framework.dao.DataAccessObject

Abstract base class for all standard data access objects using JDBC. This class provides utility methods for use in subclasses. It is a Layer Supertype [Fowler].

Users of DAOs extending this class should make sure they call the close() method on the DAO to release all resources. Normally the usage pattern will be:

 MyDAO dao=someDAOFactory.createMyDAO();
 Connection conn=myDataSource.getConnection();
 try {
     dao.init(conn);
 
     ... //use the DAO    
 
     conn.commit();
 }
 catch (DataAccessException e) {
     conn.rollback();
 }
 finally {
     dao.close();
 }
 conn.close(); //if no longer needed
 

Since DAOs extending this class are JDBC based, the init() method expects a standard JDBC connection.

Author:
Erwin Vervaet
See Also:
JDBCConnection

Field Summary
protected  java.util.Map loadedObjects
          This map serves as an Identity Map [Fowler] to make sure that each object is only loaded once.
 
Constructor Summary
protected BaseDAO(org.dartra.framework.dao.DAOFactory factory)
          Constructor used by subclasses.
 
Method Summary
 void close()
           
protected abstract  org.dartra.framework.Persistent createPersistentObject(java.lang.Object id, java.sql.ResultSet rs)
          Create a persistent domain object with given id based on the data available in the current row of given result set.
 java.lang.Object executeQuery(PreparedStatementCreator psc)
          Execute a query that returns a single result (so the result table has 1 row and 1 column).
 void executeQuery(PreparedStatementCreator psc, ResultSetRowHandler rsrh)
          Execute a query.
 java.lang.Object executeQuery(java.lang.String sql)
          Execute a static query that returns a single result (so the result table has 1 row and 1 column).
 void executeQuery(java.lang.String sql, ResultSetRowHandler rsrh)
          Execute a static query, a query with no variable parameters.
 int executeUpdate(PreparedStatementCreator psc)
          Execute an SQL update statement.
 org.dartra.framework.dao.Connection getConnection()
           
 org.dartra.framework.dao.DAOFactory getDAOFactory()
           
protected  java.lang.Object getPersistentObjectId(java.sql.ResultSet rs)
          Get the persisten store id from the current row in given result set.
 void init(org.dartra.framework.dao.Connection conn)
           
protected  org.dartra.framework.Persistent loadPersistentObject(java.sql.ResultSet rs)
          Template method to load a persistent object from the current row in given result set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

loadedObjects

protected java.util.Map loadedObjects

This map serves as an Identity Map [Fowler] to make sure that each object is only loaded once.

Constructor Detail

BaseDAO

protected BaseDAO(org.dartra.framework.dao.DAOFactory factory)

Constructor used by subclasses. This creates a new DAO, created by given DAO factory.

Method Detail

init

public void init(org.dartra.framework.dao.Connection conn)
          throws org.dartra.framework.dao.DataAccessException
Specified by:
init in interface org.dartra.framework.dao.DataAccessObject
Throws:
org.dartra.framework.dao.DataAccessException

close

public void close()
           throws org.dartra.framework.dao.DataAccessException
Specified by:
close in interface org.dartra.framework.dao.DataAccessObject
Throws:
org.dartra.framework.dao.DataAccessException

getDAOFactory

public org.dartra.framework.dao.DAOFactory getDAOFactory()
Specified by:
getDAOFactory in interface org.dartra.framework.dao.DataAccessObject

getConnection

public org.dartra.framework.dao.Connection getConnection()
Specified by:
getConnection in interface org.dartra.framework.dao.DataAccessObject

loadPersistentObject

protected org.dartra.framework.Persistent loadPersistentObject(java.sql.ResultSet rs)
                                                        throws java.sql.SQLException,
                                                               org.dartra.framework.dao.DataAccessException

Template method to load a persistent object from the current row in given result set. This method also manages the map of loaded objects.

Subclasses should redefine the getPersistentObjectId() and createPersistentObject() methods to fill out this algorithm.

Throws:
java.sql.SQLException
org.dartra.framework.dao.DataAccessException

getPersistentObjectId

protected java.lang.Object getPersistentObjectId(java.sql.ResultSet rs)
                                          throws java.sql.SQLException,
                                                 org.dartra.framework.dao.DataAccessException

Get the persisten store id from the current row in given result set.

The default implementation of this method takes the field named "ID" from the current row. Override this if necessary.

Throws:
java.sql.SQLException
org.dartra.framework.dao.DataAccessException

createPersistentObject

protected abstract org.dartra.framework.Persistent createPersistentObject(java.lang.Object id,
                                                                          java.sql.ResultSet rs)
                                                                   throws java.sql.SQLException,
                                                                          org.dartra.framework.dao.DataAccessException

Create a persistent domain object with given id based on the data available in the current row of given result set.

Throws:
java.sql.SQLException
org.dartra.framework.dao.DataAccessException

executeUpdate

public int executeUpdate(PreparedStatementCreator psc)
                  throws org.dartra.framework.dao.DataAccessException

Execute an SQL update statement.

Parameters:
psc - The callback handler that will create the prepared statement for the update.
Returns:
The number of affected rows.
Throws:
org.dartra.framework.dao.DataAccessException

executeQuery

public java.lang.Object executeQuery(java.lang.String sql)
                              throws org.dartra.framework.dao.DataAccessException

Execute a static query that returns a single result (so the result table has 1 row and 1 column). If multiple rows are returned by the query, the value of the first column in the first row is returned.

Parameters:
sql - The SQL query string.
Throws:
org.dartra.framework.dao.DataAccessException

executeQuery

public java.lang.Object executeQuery(PreparedStatementCreator psc)
                              throws org.dartra.framework.dao.DataAccessException

Execute a query that returns a single result (so the result table has 1 row and 1 column). If multiple rows are returned by the query, the value of the first column in the first row is returned.

Parameters:
psc - The callback handler that will create the prepared statement for the query.
Throws:
org.dartra.framework.dao.DataAccessException

executeQuery

public void executeQuery(java.lang.String sql,
                         ResultSetRowHandler rsrh)
                  throws org.dartra.framework.dao.DataAccessException

Execute a static query, a query with no variable parameters.

Parameters:
sql - The SQL query string.
rsrh - The callback handler that process result rows for the query.
Throws:
org.dartra.framework.dao.DataAccessException

executeQuery

public void executeQuery(PreparedStatementCreator psc,
                         ResultSetRowHandler rsrh)
                  throws org.dartra.framework.dao.DataAccessException

Execute a query.

Parameters:
psc - The callback handler that will create the prepared statement for the query.
rsrh - The callback handler that will process result rows for the query.
Throws:
org.dartra.framework.dao.DataAccessException