Rodika Systems - Articles

Home > Articles

Java DAO Generation

J2EE developers use the Data Access Object (DAO) design pattern to separate low-level data access logic from high-level business logic.

AspectData DAO for Java generates two kinds of objects.

  • DAO based on a table in the database schema. The code performs insert, update, delete, retrieve by key and search operations.

  • DAO based on a given query that return read-only data. The query can include one or more tables.

Table DAO

It is possible to create table based DAOs starting from both the schema browser or from the ER diagram. In both cases right click on the desired table and choose Generate | Java DAO. The dialog lets you customize the DAO generation.

AspectData can generate both the DAO and the associated Java bean or just the DAO. Select the root directory where you want the classes to be created, add the package names in front of the class names and modify, if neccessary, the property names and types. After you are done click OK.



























Query DAO

To generate query DAO start from the a Query Analyzer windows by editing and executing the query. Usually a query will have a number of input parameters and a result set. For instance:

SELECT EMPNO, ENAME, JOB, SAL FROM SCOTT.EMP WHERE SAL BETWEEN 800 AND 2000;

  • 800 and 2000 will become input parameters and you will be able to specify the actual values at runtime.;
  • EMPNO, ENAME etc will be properties of the output bean;



AspectData will try to guess the names and the types of the input parameters and populate the "Query Parameters" table. If you decide that some constant you used in the query should not become a parameter but remain constant (such as date formats for instance) check the "keep constant" checkbox.

The dialog shows the original query as well as the prepared statement that will be used at runtime. The DAO will create two search methods:
  • retrieveAll method uses all the input parameters
  • searchAll method uses only the parameters that are not null.

For instance

SELECT * FROM SCOTT.EMP WHERE SAL BETWEEN 200 and 2000 AND DEPTNO=30;

Will generate three input parameters, lower and upper salaries and the department number. If department number is not set (left NULL or zero in case you use primitive types) the DAOs method searchAll will only search using the salary limits. The query executed at runtime in this case will be :

SELECT * FROM SCOTT.EMP WHERE SAL BETWEEN ? and ?;

where ? will be replaced by the actual values.


Unit Tests

AspectData creates the scheleton of a unit test inside the DAO class. You have to provide the connection and the property values for the bean class in order to execute the unit test.

Modifying the generator templates

In the commercial version it is possible to modify the generator templates. The templates use velocity engine for generation.

http://jakarta.apache.org/velocity/docs/user-guide.html