4. Creating a Data Access Object (DAO)

PROBLEM

Data Access Objects (DAO) is a pattern for separate data access logic from business logic.

SOLUTION

A Data Access Object (DAO) is a Spring DSL artifact used to separate data access logic from application logic. A DAO manages the persistence of domain objects, and it is generated into @Repository annotated Spring components, a specialized stereotype for data access layer components. A DAO can also contain predefined queries called Named Queries.

HOW IT WORKS

Data Access Objects are abstract representations of real databases (or persistence mechanisms), and they manage persistence for a set of domain objects. When designing the models there is no need to know the detail of the database (vendor, version, IP, etc.), but simply a need to know that one exists for modeling database activity. When models are deployed, the developer late binds each Data Access Object to a real, physical implementation of a database.

Steps for creating a Data Access Object:

  1. Right click on a Model Package, and select New-->Data Access Object to open the New Data Access Object Wizard.

  2. From the New Data Access Object panel, enter a name for the data access object. Click Next.

  3. From the Add Domain Objects panel, select the domain objects to be managed by this DAO. Click Finish to open the Data Access Object Editor.

  4. From the Database Configuration tab of the Data Access Object Editor you can configure the DAO for a particular Eclipse datasource.

If you're familiar with JPA, then the configuration settings should look pretty familiar. These settings are directly related to JPA annotations. As you make changes to the Persistence Mapping panel, the annotations in the code associated with the domain objects will be automatically updated.