Assignment 4: Technologies and Patterns for Data Access
This is one of the choices for Assignment 4.
The Data Access Object (DAO) Pattern
Objectives: This assignment focuses on DAO pattern, understanding the DAO pattern and methods for automatic generation of DAO implementations.
As a conceptual background, all concepts related to data access technologies of different levels of abstraction and data access patterns must be known (Lectures weeks 9,10,11).
Assignment 4 variant: Automatic Generation of DAO Implementation
Stanndard Requirements
The goal of the DAOpattern is to abstract and encapsulate all access to the data source by introducing a DAO interface.
The implementation of such a DAO interface will always contain some boilerplate code, thus it can be automatically generated by tools.
There are frameworks such as Spring Data which are doing this.
For this asssignment, design and implement a (part of) a very simplified DAO generator.
Your DAO generator tool will take as input:
- a Java class (the entity that is persisted / the data transfer object). Consider the simplified case when the class has only attributes of types string and numeric.
- a connection URL to the database containing a table with a structure corresponding to the class. Consider that by default there is a correspondence between class name and table name, and class attribute names and table column names.
- a Java interface which is the DAO interface. The methods in the DAO interface must follow a strict naming convention. The DAO methods can be of types find, save or delete. The method names for find and delete type of methods must also specify the selection clause, as in findAll, deleteAll, findBy[attribute], findBy[attribute1]And[attribute2], etc. These methods will take as arguments the attribute values. For example: findByNameAndAge("John", 31). The method save takes as argument the entity/data transfer object.
Implement a DAO generator for the conditions described before. The implementation of any method with a name following the pattern findBy[attribute], having one argument attributevalue, will use a JDBC connection to the database and execute a query such as SELECT * WHERE ATTRIBUTE=ATTRIBUTEVALUE. The DAO Generator parses the method names and constructs the corresponding queries and executes them. All the query methods return lists of datatransfer objects (no ResultSets are returned !) To generate the DAOImplementation class you can use any one of the following 2 strategies (similar with generating clientside proxies for broker):
- a generator tool that generates the source code of the DAOImplementation class
- dynamic generation at runtime, using DynamicProxy
Demonstrate the use of your DAO generator in 2 different applications:
- an application dealing with Employees (name, surname, SSN, salary, age). The DAO interface should define methods such as find Employees by SSN; find by name AND surname; find by salary and age; delete by name and surname, etc.
- an application dealing with Books (title,author, year, price). DAO interface should define some appropriate methods.
Bonus Requirements
Further extension: eliminate some of the restrictions relative to the allowed forms of Java classes accepted. Accept classes that may have association relationships with other classes.
Methods in the DAO interface can include find methods that refer to attributes of associated classes using the dot notation. For example, class Order has an attribute of type Customer. You can define in the DAO interface handling Orders a method such as findByCustomerName(aName). The same query method can use values for its own attributes and attributes of associated classes, such as findByCustomerNameAndDate(aName, aDate): Date is an attribute of Order, Order is associated with Customer and Customer has an attribute Name.
Deadlines and Grading
The deadline for this assignment is in Week 13. See the Grading Policy on CV for
PASSC and
DACSS.
Frameworks/technologies to use
- JDBC
- Database: can use any relational database; recommended for easy usage without installation: H2 database
Resources
- Slides - Lecture notes weeks 9, 10, 11
link
- Source code of an example using JDBC with in-memory H2 database JDBCPersonsCars.zip
- Your competitor (not to be used here, just for inspiration): The Repository concept in Spring Data
- Source code of an example using Spring Data : SpringPersonsCars.zip